/* Mobile-first. Colors are variables so dark mode is one block. */
:root {
  --bg: #faf8f5;
  --surface: #ffffff;
  --text: #1f1d1a;
  --muted: #6b665f;
  --accent: #c2410c;
  --border: #e7e2da;
  --radius: 12px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 17px;
  line-height: 1.5;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #171513;
    --surface: #221f1c;
    --text: #f2eee8;
    --muted: #a39d94;
    --accent: #fb923c;
    --border: #36322d;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
}

.topbar {
  position: sticky;
  top: 0;
  padding: 12px 16px;
  padding-top: max(12px, env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.brand {
  color: var(--text);
  font-weight: 700;
  font-size: 1.2rem;
  text-decoration: none;
}

.page {
  max-width: 900px;
  margin: 0 auto;
  padding: 16px;
}

h1 { margin: 0 0 8px; font-size: 1.6rem; }

.muted { color: var(--muted); }

a { color: var(--accent); }
.small { font-size: 0.85rem; }
.error { color: #dc2626; }
@media (prefers-color-scheme: dark) { .error { color: #f87171; } }

button, .add-form input {
  font: inherit;
  min-height: 44px;            /* comfortable thumb target */
  border-radius: var(--radius);
}
button {
  border: 0;
  padding: 0 18px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
}
.small-btn { min-height: 34px; padding: 0 12px; margin-top: 6px; font-size: 0.9rem; }

.add-form { display: flex; gap: 8px; margin-bottom: 16px; }
.add-form input {
  flex: 1;
  min-width: 0;
  padding: 0 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}

.badge {
  display: inline-block;
  margin-top: 4px;
  padding: 2px 10px;
  border-radius: 999px;
  background: var(--border);
  font-size: 0.85rem;
}

.search-form { margin-bottom: 12px; }
.search-form input[type=search] {
  width: 100%;
  min-height: 44px;
  padding: 0 12px;
  font: inherit;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
}
.chips { display: flex; gap: 8px; margin-top: 10px; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 36px;
  padding: 0 14px;
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--text);
  text-decoration: none;
  font-size: 0.95rem;
}
.chip.on { background: var(--text); color: var(--bg); border-color: var(--text); }
.chip .count {
  min-width: 22px;
  padding: 0 6px;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-size: 0.8rem;
  text-align: center;
}

.grid-meta { color: var(--muted); font-size: 0.85rem; margin-bottom: 8px; min-height: 1.2em; }
.empty { padding: 24px 0; text-align: center; }

/* 2 cards across on a phone, more on wider screens */
.grid {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 12px;
}
.card {
  position: relative;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
/* Whole card is tappable; buttons/tags inside sit above this link */
.card-link { position: absolute; inset: 0; z-index: 1; }
.card-action { position: relative; z-index: 2; }

.card-thumb {
  position: relative;
  aspect-ratio: 3 / 4;
  background: var(--border);
}
.no-thumb::before {
  content: "🍽";
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2.5rem;
  opacity: 0.35;
}
.card-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.45);
  color: #fff;
  font-weight: 600;
  font-size: 0.9rem;
}
.status-downloading .overlay { animation: pulse 1.6s ease-in-out infinite; }
@keyframes pulse { 50% { background: rgba(0, 0, 0, 0.25); } }
.overlay-failed { background: rgba(153, 27, 27, 0.6); }
.flag {
  position: absolute;
  left: 8px;
  bottom: 8px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-size: 0.75rem;
  font-weight: 600;
}

.card-body { padding: 8px 10px 10px; min-width: 0; }
.card-title {
  font-weight: 600;
  line-height: 1.3;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  overflow-wrap: anywhere;
}
.card-error {
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  margin-top: 4px;
}
.tags { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; align-items: center; }
.tag {
  padding: 1px 8px;
  border-radius: 999px;
  background: var(--border);
  color: var(--text);
  font-size: 0.75rem;
  text-decoration: none;
}

/* ---------- Recipe page ---------- */
.back { display: inline-block; margin-bottom: 12px; color: var(--muted); text-decoration: none; }

.video, .video-placeholder {
  display: block;
  width: 100%;
  max-height: 55vh;
  aspect-ratio: 9 / 16;
  margin: 0 auto 16px;
  background: #000;
  border-radius: var(--radius);
}
.video-placeholder {
  max-height: 240px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-items: center;
  justify-content: center;
  padding: 16px;
  text-align: center;
  background: var(--surface);
  border: 1px solid var(--border);
}

.byline { margin: 0 0 12px; }
.pre { white-space: pre-line; overflow-wrap: anywhere; }

.facts { display: flex; flex-wrap: wrap; gap: 8px; margin: 0 0 12px; }
.facts div {
  padding: 6px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.facts dt { font-size: 0.75rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.facts dd { margin: 0; font-weight: 600; }

.recipe-body .tags { margin: 0 0 12px; }
.recipe-body .tag { font-size: 0.85rem; padding: 3px 10px; }

.callout {
  padding: 12px 14px;
  border-radius: var(--radius);
  background: color-mix(in srgb, var(--accent) 14%, var(--surface));
  border: 1px solid color-mix(in srgb, var(--accent) 35%, var(--border));
}

.wake-btn {
  min-height: 40px;
  margin-bottom: 8px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  font-weight: 500;
}
.wake-btn[aria-pressed="true"] { background: #facc15; color: #1f1d1a; border-color: #facc15; }

.section { margin-top: 24px; }
.section h2 { margin: 0 0 8px; font-size: 1.25rem; }
.section-head { display: flex; align-items: baseline; justify-content: space-between; }
.link-btn {
  min-height: 32px;
  padding: 0;
  background: none;
  color: var(--accent);
  font-weight: 500;
}

/* Ingredients: whole row is the tap target */
.ingredients { list-style: none; margin: 0; padding: 0; }
.ingredients li { border-bottom: 1px solid var(--border); }
.ingredients label {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  padding: 12px 2px;
  cursor: pointer;
}
.ingredients input {
  flex: 0 0 auto;
  width: 24px;
  height: 24px;
  margin: 1px 0 0;
  accent-color: var(--accent);
}
.ingredients input:checked + span { color: var(--muted); text-decoration: line-through; }

/* Steps: tap to mark done */
.steps { list-style: none; margin: 0; padding: 0; }
.steps li {
  display: flex;
  gap: 12px;
  padding: 12px;
  margin-bottom: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
}
.step-num {
  flex: 0 0 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
}
.step-text { white-space: pre-line; overflow-wrap: anywhere; }
.steps li.done { opacity: 0.5; }
.steps li.done .step-num { background: var(--muted); }
.steps li.done .step-text { text-decoration: line-through; }

.caption-box summary { cursor: pointer; color: var(--muted); }
.source { margin-top: 24px; }

/* Laptop: video on the left stays put while the recipe scrolls */
@media (min-width: 820px) {
  .recipe { display: grid; grid-template-columns: 340px 1fr; gap: 32px; align-items: start; }
  .recipe-media { position: sticky; top: 72px; }
  .video { max-height: calc(100vh - 100px); }
}
.status-failed .no-thumb::before,
.status-downloading .no-thumb::before { display: none; }

/* ---------- Editor ---------- */
.page-top { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.page-top .back { margin: 0; }
.edit-btn, .callout-btn {
  display: inline-flex;
  align-items: center;
  min-height: 36px;
  padding: 0 14px;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  text-decoration: none;
}
.callout-btn { margin-top: 8px; }

.editor input, .editor textarea {
  width: 100%;
  min-height: 44px;
  padding: 8px 10px;
  font: inherit;
  font-size: 16px;            /* 16px+ stops iPhone zooming into the field */
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.editor textarea { resize: vertical; line-height: 1.4; }
.editor input:focus, .editor textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.field { display: block; margin-bottom: 12px; }
.field > span { display: block; font-weight: 600; font-size: 0.9rem; margin-bottom: 4px; }
.field-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }

.caption-ref {
  margin-bottom: 16px;
  padding: 10px 12px;
  background: var(--surface);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
}
.caption-ref summary { cursor: pointer; color: var(--muted); }
.caption-ref p { margin: 8px 0 0; font-size: 0.95rem; }

.quick-tags { margin: -4px 0 12px; }
.quick-tags .tag { border: 0; min-height: 30px; font: inherit; font-size: 0.85rem; cursor: pointer; }

.ed-list { list-style: none; margin: 0; padding: 0; }
.ed-row {
  padding: 8px;
  margin-bottom: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.ing-line { display: flex; gap: 6px; }
.ing-line + .ing-line { margin-top: 6px; }
.ing-line input[name=ing_qty] { flex: 0 0 64px; }
.ing-line input[name=ing_unit] { flex: 0 0 76px; }
.ing-line input[name=ing_item], .ing-line input[name=ing_note] { flex: 1; min-width: 0; }

.step-row { display: flex; gap: 8px; align-items: flex-start; }
.step-row .step-num { margin-top: 8px; }
.steps-editor, #steps { counter-reset: step; }
#steps .step-num::before { counter-increment: step; content: counter(step); }
.step-row textarea { flex: 1; min-width: 0; }

.row-tools { display: flex; gap: 4px; flex: 0 0 auto; }
.row-tools.vertical { flex-direction: column; }
.tool {
  min-height: 40px;
  min-width: 40px;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  font-weight: 500;
}
.row-tools.vertical .tool { min-height: 32px; }
.tool.remove { color: #dc2626; }

.add-btn {
  width: 100%;
  background: none;
  color: var(--accent);
  border: 1px dashed var(--border);
}
.paste-box { margin-top: 10px; }
.paste-box summary { cursor: pointer; color: var(--accent); font-weight: 500; min-height: 32px; }
.paste-box textarea { margin-top: 6px; }
.paste-box p { margin: 4px 0 0; }

/* Save stays reachable at the bottom of the screen while you scroll */
.save-bar {
  position: sticky;
  bottom: 0;
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  align-items: center;
  margin: 24px -16px 0;
  padding: 12px 16px;
  padding-bottom: max(12px, env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
}
.save-bar .cancel { color: var(--muted); text-decoration: none; padding: 0 12px; }
.save-bar button { flex: 0 1 200px; }

.danger-zone { margin: 32px 0 16px; }
.danger-zone summary { cursor: pointer; color: #dc2626; }
.danger-btn { background: #dc2626; margin-top: 4px; }

/* ---------- Sign-in & settings ---------- */
.topbar { display: flex; justify-content: space-between; align-items: center; }
.settings-link {
  color: var(--muted);
  text-decoration: none;
  font-size: 1.4rem;
  min-width: 44px;
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
}
.login { max-width: 360px; margin: 32px auto; text-align: center; }
.login .field, .login p { text-align: left; }
.login-icon { border-radius: 16px; margin-bottom: 8px; }
.wide-btn { width: 100%; }
.secondary-btn { background: var(--surface); color: var(--text); border: 1px solid var(--border); }
.copy-row { display: flex; gap: 8px; }
.copy-row input {
  flex: 1;
  min-width: 0;
  min-height: 44px;
  padding: 0 10px;
  font: inherit;
  font-size: 15px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.copy-btn { flex: 0 0 auto; min-width: 84px; }
code { font-size: 0.9em; padding: 1px 4px; border-radius: 4px; background: var(--border); }

/* ---------- Sections (library tabs) ---------- */
.tabs {
  display: flex;
  gap: 4px;
  margin: -4px -16px 14px;
  padding: 0 16px;
  overflow-x: auto;
  scrollbar-width: none;
  border-bottom: 1px solid var(--border);
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 44px;
  padding: 0 12px;
  color: var(--muted);
  font-weight: 600;
  text-decoration: none;
  border-bottom: 3px solid transparent;
  margin-bottom: -1px;
}
.tab.on { color: var(--text); border-bottom-color: var(--accent); }
.tab-count { font-size: 0.8rem; font-weight: 500; color: var(--muted); }
.tab-edit { margin-left: auto; font-weight: 400; }

.section-toggles { display: flex; flex-wrap: wrap; gap: 8px; margin: 0 0 12px; }
.section-toggles form { margin: 0; }
.section-toggle {
  min-height: 36px;
  padding: 0 14px;
  border-radius: 999px;
  background: var(--surface);
  color: var(--text);
  border: 1px dashed var(--border);
  font-weight: 500;
}
.section-toggle.on { background: var(--accent); color: #fff; border: 1px solid var(--accent); }

.section-picks { border: 0; padding: 0; margin: 0 0 12px; }
.section-picks legend { font-weight: 600; font-size: 0.9rem; margin-bottom: 4px; padding: 0; }
.section-pick {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  margin-right: 16px;
  cursor: pointer;
}
.section-pick input { width: 22px; height: 22px; min-height: 0; accent-color: var(--accent); }

.section-list { margin: 16px 0; }
.rename-form { display: flex; gap: 8px; }
.rename-form input { flex: 1; min-width: 0; }
.rename-form .small-btn { margin: 0; min-height: 44px; }
.section-row-tools { display: flex; justify-content: space-between; align-items: center; margin-top: 8px; }
.section-row-tools form { margin: 0; }
.tool:disabled { opacity: 0.35; }
.small-danger { margin: 8px 0 0; font-size: 0.9rem; }

/* ---------- Auto-fill ---------- */
.callout.suggest {
  background: color-mix(in srgb, #7c3aed 12%, var(--surface));
  border-color: color-mix(in srgb, #7c3aed 35%, var(--border));
}
.flag-suggest { background: #7c3aed; }
.ed-row.suggested { border-left: 4px solid #7c3aed; }
.recipe-body form .secondary-btn { margin: 0 0 12px; }
