:root {
  --bg: #f4f7f1;
  --card: #ffffff;
  --accent: #4a8b5c;
  --accent-dark: #2f6b40;
  --accent-soft: #e0efe0;
  --text: #202821;
  --muted: #71806f;
  --border: #dde6d8;
  --shadow: 0 1px 2px rgba(30, 50, 30, 0.05), 0 6px 16px rgba(30, 50, 30, 0.07);
  --shadow-lg: 0 10px 30px rgba(20, 40, 20, 0.16);
  --radius: 14px;
  --font-display: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141a13;
    --card: #1d251b;
    --accent: #6bb37e;
    --accent-dark: #8fcc9e;
    --accent-soft: #2a3826;
    --text: #eaf1e6;
    --muted: #9ab095;
    --border: #33402f;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 6px 16px rgba(0, 0, 0, 0.35);
    --shadow-lg: 0 10px 30px rgba(0, 0, 0, 0.5);
  }
}

* { box-sizing: border-box; }

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

header {
  background: var(--accent-dark);
  background-image: linear-gradient(160deg, var(--accent) 0%, var(--accent-dark) 100%);
  color: white;
  padding: 14px 16px 12px;
  position: sticky;
  top: 0;
  z-index: 5;
  box-shadow: var(--shadow);
}

header h1 {
  margin: 0 0 12px;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 1.4rem;
  letter-spacing: 0.2px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.logo-mark {
  display: inline-block;
  filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.15));
}

.filters-row, .actions-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.filters-row { margin-bottom: 8px; }

.filters-row input, .filters-row select,
.actions-row button {
  padding: 8px 12px;
  border-radius: 999px;
  border: none;
  font-size: 0.88rem;
}

.filters-row input, .filters-row select {
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.94);
  color: var(--text);
}

.actions-row button {
  background: rgba(255, 255, 255, 0.16);
  color: white;
  cursor: pointer;
  font-weight: 500;
  transition: background 0.15s ease, transform 0.1s ease;
}

.actions-row button:hover { background: rgba(255, 255, 255, 0.26); }
.actions-row button:active { transform: scale(0.97); }

#search { flex: 1 1 200px; }

.primary-btn {
  background: white !important;
  color: var(--accent-dark) !important;
  font-weight: 700 !important;
}

main {
  max-width: 960px;
  margin: 0 auto;
  padding: 20px 16px 40px;
}

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

.cuisine-flag {
  display: inline-block;
  margin-right: 6px;
  animation: flag-wave 2.2s ease-in-out infinite;
  transform-origin: 70% 70%;
}

@keyframes flag-wave {
  0%, 100% { transform: rotate(0deg); }
  25% { transform: rotate(-12deg); }
  75% { transform: rotate(12deg); }
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  cursor: pointer;
  position: relative;
  box-shadow: var(--shadow);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-lg);
}

.card h3 {
  margin: 0 0 6px;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 1.12rem;
  line-height: 1.3;
  padding-right: 82px;
}
.card .meta { color: var(--muted); font-size: 0.85rem; }

.card-photo {
  width: calc(100% + 28px);
  margin: -14px -14px 10px;
  max-height: 140px;
  object-fit: cover;
  border-radius: var(--radius) var(--radius) 0 0;
  display: block;
}

.card-toggles {
  position: absolute;
  top: 10px;
  right: 10px;
  display: flex;
  gap: 6px;
}

.fav-toggle, .cart-toggle, .today-toggle {
  background: none;
  border: none;
  font-size: 1.2rem;
  cursor: pointer;
  line-height: 1;
  border-radius: 50%;
  opacity: 0.45;
}

.today-toggle.active {
  opacity: 1;
  background: var(--accent-soft);
}

.detail-photo {
  width: calc(100% + 40px);
  margin: -20px -20px 14px;
  max-height: 260px;
  object-fit: cover;
  display: block;
}

.form-photo-preview {
  max-width: 100%;
  max-height: 160px;
  border-radius: 8px;
  margin-bottom: 6px;
  display: block;
}

.cart-recipe-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.cart-recipe-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
}

.cart-recipe-list button {
  border: none;
  background: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 1rem;
}

.today-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.today-list li {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 4px 10px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}

.today-name {
  font-weight: 600;
  cursor: pointer;
  flex: 1 1 auto;
}

.today-list .meta {
  color: var(--muted);
  font-size: 0.85rem;
  cursor: pointer;
  flex: 1 1 100%;
}

.today-actions {
  display: flex;
  gap: 8px;
}

.today-actions button {
  border: none;
  background: none;
  font-size: 1.1rem;
  cursor: pointer;
}

.shopping-group {
  margin-top: 14px;
  white-space: normal;
}

.shopping-group h4 {
  margin: 0 0 6px;
  color: var(--accent-dark);
  font-family: var(--font-display);
  font-weight: 600;
}

.shopping-checklist {
  list-style: none;
  padding: 0;
  margin: 0 0 10px;
}

.shopping-checklist li label {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 0;
  cursor: pointer;
}

.shopping-checklist li.checked span {
  text-decoration: line-through;
  color: var(--muted);
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(20, 12, 5, 0.5);
  backdrop-filter: blur(2px);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 24px 12px;
  overflow-y: auto;
  z-index: 20;
}

.modal {
  background: var(--card);
  color: var(--text);
  border-radius: 18px;
  padding: 22px;
  max-width: 560px;
  width: 100%;
  box-shadow: var(--shadow-lg);
}

.modal h2 {
  margin-top: 0;
  font-family: var(--font-display);
  font-weight: 600;
}
.modal .section { margin-top: 14px; white-space: pre-wrap; }
.modal .section h4 {
  margin: 0 0 6px;
  color: var(--accent-dark);
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 1rem;
}

.smart-add {
  background: var(--bg);
  border: 1px dashed var(--accent);
  border-radius: 10px;
  padding: 12px;
  margin-bottom: 16px;
}

.smart-add label {
  display: block;
  font-size: 0.85rem;
  color: var(--muted);
  margin-bottom: 6px;
}

.smart-add textarea, .smart-add input {
  width: 100%;
  min-height: 100px;
  padding: 8px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  font-family: inherit;
  font-size: 0.9rem;
}

.smart-add input { min-height: auto; }

.smart-add .modal-actions { margin-top: 8px; }
#smartStatus { font-size: 0.85rem; color: var(--muted); margin-top: 6px; }

.tab-row {
  display: flex;
  gap: 6px;
  margin-bottom: 14px;
  border-bottom: 1px solid var(--border);
  padding-bottom: 10px;
}

.tab-btn {
  padding: 6px 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  cursor: pointer;
  font-size: 0.9rem;
}

.tab-btn.active {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 16px;
}

.modal-actions button {
  padding: 8px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  cursor: pointer;
  transition: border-color 0.15s ease;
}

.modal-actions button:hover { border-color: var(--accent); }

.modal-actions .danger-btn {
  border-color: #d9534f;
  color: #d9534f;
  margin-right: auto;
}

form.recipe-form label {
  display: block;
  margin-top: 10px;
  font-size: 0.85rem;
  color: var(--muted);
}

form.recipe-form input, form.recipe-form select, form.recipe-form textarea {
  width: 100%;
  padding: 8px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  font-size: 0.95rem;
  font-family: inherit;
}

form.recipe-form textarea { min-height: 80px; }

.chat-fab {
  position: fixed;
  bottom: 20px;
  right: 20px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--accent);
  color: white;
  border: none;
  font-size: 1.4rem;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(0,0,0,0.25);
  z-index: 15;
}

.chat-panel {
  position: fixed;
  bottom: 90px;
  right: 20px;
  width: min(340px, calc(100vw - 40px));
  max-height: 60vh;
  background: var(--card);
  color: var(--text);
  border-radius: 16px;
  box-shadow: var(--shadow-lg);
  display: none;
  flex-direction: column;
  z-index: 15;
  overflow: hidden;
}

.chat-panel.open { display: flex; }

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  font-size: 0.9rem;
}

.chat-messages .msg { margin-bottom: 10px; white-space: pre-wrap; }
.chat-messages .msg.user { color: var(--accent-dark); font-weight: 600; }

.chat-input {
  display: flex;
  border-top: 1px solid var(--border);
}

.chat-input input {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text);
  padding: 10px;
  font-size: 0.9rem;
}

.chat-input button {
  border: none;
  background: var(--accent);
  color: white;
  padding: 0 16px;
  cursor: pointer;
}

.hint {
  color: var(--accent-dark);
  font-size: 0.85rem;
  font-weight: 600;
  margin: 0 0 10px;
}

.empty {
  text-align: center;
  color: var(--muted);
  margin-top: 40px;
}

#receiptResult label, #pricesBackdrop label {
  display: block;
  margin-top: 10px;
  font-size: 0.85rem;
  color: var(--muted);
}

#receiptResult > input, #pricesBackdrop > .modal > input {
  width: 100%;
  padding: 8px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  font-size: 0.95rem;
}

.receipt-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 14px;
  font-size: 0.9rem;
}

.receipt-table th {
  text-align: left;
  color: var(--muted);
  font-weight: 600;
  padding: 6px 4px;
  border-bottom: 1px solid var(--border);
}

.receipt-table td {
  padding: 4px;
  border-bottom: 1px solid var(--border);
}

.receipt-table input {
  width: 100%;
  padding: 6px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  font-size: 0.85rem;
}

.receipt-table .ri-price, .receipt-table .ri-qty { width: 70px; }
.receipt-table .ri-unit { width: 50px; }

.receipt-table .ri-remove {
  border: none;
  background: none;
  color: var(--muted);
  cursor: pointer;
}

.price-chart {
  width: 100%;
  height: auto;
  margin-top: 10px;
  background: var(--bg);
  border-radius: 8px;
}

.cost-badge {
  display: inline-block;
  margin-top: 6px;
  padding: 3px 10px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent-dark);
  font-size: 0.8rem;
  font-weight: 600;
}

.info-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 1.5px solid white;
  background: none;
  color: white;
  font-size: 0.8rem;
  font-style: italic;
  font-weight: 700;
  cursor: pointer;
  vertical-align: middle;
  margin-left: 4px;
}

.changelog-list {
  margin: 0 0 4px;
  padding-left: 18px;
}

.changelog-list li {
  font-size: 0.9rem;
  margin-bottom: 4px;
}

.app-credit {
  text-align: center;
  color: var(--muted);
  font-size: 0.85rem;
  margin-top: 18px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}

.plan-day {
  border-bottom: 1px solid var(--border);
  padding: 10px 0;
}

.plan-day h4 {
  margin: 0 0 6px;
  color: var(--accent-dark);
  font-family: var(--font-display);
  font-weight: 600;
}

.plan-day-list {
  list-style: none;
  padding: 0;
  margin: 0 0 6px;
}

.plan-day-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 3px 0;
  font-size: 0.9rem;
}

.plan-day-list button {
  border: none;
  background: none;
  color: var(--muted);
  cursor: pointer;
}

.plan-add-input {
  width: 100%;
  padding: 6px;
  border-radius: 6px;
  border: 1px dashed var(--border);
  background: var(--card);
  color: var(--text);
  font-size: 0.85rem;
}
