:root {
  --bg: #f4f6f8;
  --card-bg: #ffffff;
  --text: #1f2933;
  --muted: #64748b;
  --border: #e2e8f0;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --danger: #dc2626;
  --danger-bg: #fef2f2;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
[hidden] { display: none !important; }

body {
  font-family: -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  padding-bottom: env(safe-area-inset-bottom);
}

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

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  padding: calc(14px + env(safe-area-inset-top)) 14px 12px;
  background: var(--card-bg);
  border-bottom: 1px solid var(--border);
}

.topbar h1 {
  margin: 0 0 10px;
  font-size: 1.3rem;
  text-align: center;
}

.toolbar {
  display: grid;
  grid-template-columns: 1fr 1.25fr 1fr;
  gap: 8px;
  max-width: 640px;
  margin: 0 auto;
}

.toolbar-btn, .primary-btn, .secondary-btn {
  min-height: 44px;
  border-radius: 11px;
  padding: 10px 12px;
  font-weight: 700;
  cursor: pointer;
}

.toolbar-btn, .secondary-btn {
  border: 1px solid var(--border);
  color: var(--text);
  background: var(--card-bg);
}

.toolbar-btn.primary, .primary-btn {
  border: 1px solid var(--primary);
  color: #fff;
  background: var(--primary);
}

.toolbar-btn:active, .secondary-btn:active { background: #eef2f7; }
.toolbar-btn.primary:active, .primary-btn:active { background: var(--primary-dark); }
.primary-btn:disabled { opacity: .65; cursor: wait; }

.search-panel {
  max-width: 640px;
  margin: 10px auto 0;
}

.search-panel > label {
  display: block;
  margin-bottom: 5px;
  color: var(--muted);
  font-size: .82rem;
  font-weight: 700;
}

.search-row { display: flex; gap: 8px; }
.search-row input { flex: 1; min-width: 0; }
.search-row button {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  padding: 0 14px;
  cursor: pointer;
}

input, textarea, select {
  width: 100%;
  border: 1px solid #cbd5e1;
  border-radius: 10px;
  background: #fff;
  color: var(--text);
  padding: 11px 12px;
}

textarea { resize: vertical; line-height: 1.4; }
input:focus, textarea:focus, select:focus {
  outline: 3px solid rgba(37, 99, 235, .15);
  border-color: var(--primary);
}

.recipe-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 14px;
  max-width: 640px;
  margin: 0 auto;
}

.recipe-card {
  position: relative;
  width: 100%;
  text-align: left;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 16px 58px 16px 16px;
  cursor: pointer;
}

.recipe-card:active { background: #eef2f7; }
.recipe-card:focus-visible { outline: 3px solid rgba(37, 99, 235, .3); }

.delete-btn {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 38px;
  height: 38px;
  border: 1px solid #fecaca;
  border-radius: 50%;
  background: var(--danger-bg);
  color: var(--danger);
  font-size: 1.2rem;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
}

.delete-btn:active { background: #fee2e2; }

.recipe-name { font-size: 1.08rem; font-weight: 700; margin-bottom: 6px; }
.recipe-tags { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 8px; }
.tag {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px 10px;
  font-size: .8rem;
  color: var(--muted);
}
.recipe-macros { font-size: .85rem; color: var(--muted); line-height: 1.5; }
.empty-state { max-width: 640px; margin: 40px auto; padding: 0 20px; text-align: center; color: var(--muted); }

.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgba(15, 23, 42, .55);
}

.modal {
  position: relative;
  width: 100%;
  max-width: 640px;
  max-height: 90vh;
  overflow-y: auto;
  border-radius: 20px 20px 0 0;
  background: var(--card-bg);
  padding: 24px 20px calc(28px + env(safe-area-inset-bottom));
}

.modal-close {
  position: absolute;
  top: 12px;
  right: 12px;
  z-index: 1;
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 50%;
  background: var(--bg);
  color: var(--muted);
  font-size: 1.4rem;
  line-height: 1;
  cursor: pointer;
}

.modal h2 { margin: 0 0 6px; padding-right: 44px; font-size: 1.25rem; }
.modal h3 { margin: 16px 0 8px; color: var(--primary-dark); font-size: 1rem; }
.modal-meta {
  margin-bottom: 18px;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--border);
  color: var(--muted);
  font-size: .88rem;
  line-height: 1.5;
}
.modal ul, .modal ol { margin: 0; padding-left: 22px; }
.modal li { margin-bottom: 8px; font-size: .95rem; line-height: 1.45; }

.add-modal label {
  display: block;
  margin: 12px 0;
  color: #334155;
  font-size: .86rem;
  font-weight: 700;
}
.add-modal label input, .add-modal label textarea, .add-modal label select { margin-top: 5px; font-weight: 400; }
.form-help { margin: 4px 0 14px; color: var(--muted); font-size: .88rem; line-height: 1.4; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; }
.form-grid label:last-child { grid-column: 1 / -1; }
.macro-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; }
.manual-macros { margin-top: 16px; padding: 12px; border: 1px solid var(--border); border-radius: 12px; background: var(--bg); }
.manual-macros h3 { margin-top: 0; }
.form-status { min-height: 1.4em; color: var(--danger); font-size: .88rem; line-height: 1.4; }
.form-status.success { color: #15803d; }
.form-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 14px; }

.ingredient-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.ingredient-actions button { width: 100%; }
.scanner-attribution {
  margin: 6px 0 14px;
  color: var(--muted);
  font-size: .76rem;
  text-align: center;
}
.scanner-attribution a { color: var(--primary); }
.scanner-modal > label {
  display: block;
  margin: 12px 0;
  color: #334155;
  font-size: .86rem;
  font-weight: 700;
}
.scanner-modal > label input { margin-top: 5px; font-weight: 400; }
.barcode-reader {
  min-height: 230px;
  overflow: hidden;
  border: 2px dashed #94a3b8;
  border-radius: 14px;
  background: #0f172a;
}
.barcode-reader:empty::before {
  content: "Camera preview";
  display: grid;
  min-height: 230px;
  place-items: center;
  color: #cbd5e1;
}
.barcode-reader video { border-radius: 12px; }
.manual-barcode {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: end;
  gap: 10px;
  margin-top: 14px;
}
.manual-barcode label {
  margin: 0;
  color: #334155;
  font-size: .86rem;
  font-weight: 700;
}
.manual-barcode label input { margin-top: 5px; font-weight: 400; }
.manual-barcode button { min-width: 100px; }
.ingredient-summary {
  margin: 14px 0;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg);
}
.ingredient-summary h3 { margin-top: 0; }
.ingredient-entry {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 9px 0;
  border-bottom: 1px solid var(--border);
}
.ingredient-entry strong, .ingredient-entry span { display: block; }
.ingredient-entry span { margin-top: 2px; color: var(--muted); font-size: .8rem; }
.ingredient-entry button {
  width: 32px;
  height: 32px;
  border: 1px solid #fecaca;
  border-radius: 50%;
  background: var(--danger-bg);
  color: var(--danger);
  font-size: 1.1rem;
  cursor: pointer;
}
.ingredient-total { margin: 12px 0 0; color: var(--muted); font-size: .84rem; line-height: 1.5; }
.ingredient-modal label {
  display: block;
  margin: 12px 0;
  color: #334155;
  font-size: .86rem;
  font-weight: 700;
}
.ingredient-modal label input { margin-top: 5px; font-weight: 400; }

@media (min-width: 700px) {
  .modal-overlay { align-items: center; padding: 20px; }
  .modal { border-radius: 16px; padding: 28px; }
  .form-grid { grid-template-columns: 1fr 1fr 1fr; }
  .form-grid label:last-child { grid-column: auto; }
  .macro-grid { grid-template-columns: repeat(4, 1fr); }
}

@media (max-width: 420px) {
  .toolbar-btn { padding: 9px 6px; font-size: .84rem; }
  .form-actions { flex-direction: column-reverse; }
  .form-actions button { width: 100%; }
  .manual-barcode { grid-template-columns: 1fr; }
  .manual-barcode button { width: 100%; }
}
