:root {
  --paper: #fbf6ec;
  --paper-2: #f3ebdc;
  --desk: #e7dcc8;
  --ink: #2a2420;
  --ink-2: #6e6156;
  --line: #dccfb9;
  --accent: #a8452a;
  --accent-ink: #fff8f0;
  --low: #4f7a3a;
  --med: #a86a12;
  --high: #b23a2e;
  --shadow: 0 1px 2px rgb(60 40 20 / 0.08), 0 10px 28px rgb(60 40 20 / 0.12);
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --paper: #27221d;
    --paper-2: #302a23;
    --desk: #181512;
    --ink: #efe6d8;
    --ink-2: #ab9e8e;
    --line: #443a30;
    --accent: #e2835f;
    --accent-ink: #1b1512;
    --low: #93c276;
    --med: #e2ad52;
    --high: #f08273;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.4), 0 10px 28px rgb(0 0 0 / 0.4);
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--desk);
  color: var(--ink);
  font: 16px/1.5 var(--sans);
}
h1, h2, h3 { font-family: var(--serif); font-weight: 600; line-height: 1.2; margin: 0; text-wrap: balance; }
a { color: var(--accent); }
img { display: block; max-width: 100%; }
button, input, textarea, select { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* ---------- Controls ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 0.4em;
  padding: 0.5em 0.95em; border-radius: 999px;
  border: 1px solid var(--line); background: var(--paper); color: var(--ink);
  text-decoration: none; cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: var(--ink-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--high); }
.btn[disabled] { opacity: 0.55; cursor: progress; }
.btn.on { background: var(--paper-2); border-color: var(--ink-2); }
.link { background: none; border: 0; padding: 0; color: var(--accent); cursor: pointer; text-decoration: underline; }
.icon { background: none; border: 0; padding: 0.25em 0.45em; border-radius: 6px; cursor: pointer; color: var(--ink-2); line-height: 1; }
.icon:hover { background: var(--paper-2); color: var(--ink); }
input[type="text"], input[type="search"], input[type="url"], input[type="number"], input[type="password"], textarea {
  width: 100%; padding: 0.55em 0.7em; border-radius: 8px;
  border: 1px solid var(--line); background: var(--paper); color: var(--ink);
}
textarea { resize: vertical; min-height: 5em; line-height: 1.45; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  padding: 0.8rem clamp(1rem, 4vw, 2.5rem);
}
.brand { font: 600 1.45rem var(--serif); color: var(--ink); text-decoration: none; letter-spacing: 0.01em; }
.brand::before { content: "❦ "; color: var(--accent); }
.topbar-actions { display: flex; gap: 0.5rem; align-items: center; }

main { padding: 0 clamp(1rem, 4vw, 2.5rem) 4rem; max-width: 1180px; margin: 0 auto; }

/* ---------- Sign in ---------- */
.login {
  max-width: 22rem; margin: 14vh auto 0; padding: 2rem; text-align: center;
  background: var(--paper); border-radius: 10px; box-shadow: var(--shadow);
}
.login h1 { font-size: 1.9rem; margin-bottom: 0.3rem; }
.login p { color: var(--ink-2); margin: 0 0 1.2rem; }
.login form { display: grid; gap: 0.7rem; }
.login .btn { justify-content: center; }

/* ---------- Library ---------- */
.toolbar { display: flex; flex-wrap: wrap; gap: 0.75rem; align-items: center; margin: 0.5rem 0 1.25rem; }
.toolbar input { flex: 1 1 16rem; max-width: 26rem; }
.chips { display: flex; flex-wrap: wrap; gap: 0.4rem; }
.chip {
  padding: 0.3em 0.85em; border-radius: 999px; border: 1px solid var(--line);
  background: transparent; cursor: pointer; color: var(--ink-2);
}
.chip[aria-pressed="true"] { background: var(--ink); color: var(--paper); border-color: var(--ink); }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(15rem, 1fr)); gap: 1.25rem; }
.card {
  display: flex; flex-direction: column; overflow: hidden;
  background: var(--paper); color: var(--ink); text-decoration: none;
  border-radius: 8px; box-shadow: var(--shadow); transition: transform 0.15s ease;
}
.card:hover { transform: translateY(-3px) rotate(-0.3deg); }
.card-photo { aspect-ratio: 4 / 3; width: 100%; object-fit: cover; background: var(--paper-2); }
.card-nophoto {
  aspect-ratio: 4 / 3; display: grid; place-items: center;
  font: 600 4rem var(--serif); color: var(--line);
  background: repeating-linear-gradient(0deg, var(--paper-2) 0 27px, var(--line) 27px 28px);
}
.card-body { padding: 0.8rem 1rem 1rem; display: grid; gap: 0.35rem; }
.card h3 { font-size: 1.15rem; }
.meta { color: var(--ink-2); font-size: 0.875rem; margin: 0; }
.badges { display: flex; flex-wrap: wrap; gap: 0.35rem; margin-top: 0.15rem; }
.badge {
  font-size: 0.75rem; padding: 0.1em 0.6em; border-radius: 999px;
  border: 1px solid var(--line); color: var(--ink-2); font-variant-numeric: tabular-nums;
}
.badge.low { color: var(--low); border-color: currentColor; }
.badge.medium { color: var(--med); border-color: currentColor; }
.badge.high { color: var(--high); border-color: currentColor; }
.badge.star { color: var(--accent); border-color: currentColor; }
.empty { grid-column: 1 / -1; text-align: center; padding: 4rem 1rem; color: var(--ink-2); }
.empty h2 { color: var(--ink); margin-bottom: 0.4rem; }

/* ---------- Recipe: an open book ---------- */
.book {
  position: relative; display: grid; grid-template-columns: 1fr 1fr;
  background: var(--paper); border-radius: 8px; box-shadow: var(--shadow);
}
.book::after {
  content: ""; position: absolute; inset: 0 auto 0 50%; width: 46px; transform: translateX(-50%);
  background: linear-gradient(90deg, transparent, rgb(0 0 0 / 0.09) 50%, transparent); pointer-events: none;
}
.page { padding: clamp(1.25rem, 3vw, 2.5rem); min-width: 0; }
.page h1 { font-size: clamp(1.7rem, 3.2vw, 2.4rem); margin: 0.2rem 0 0.5rem; }
.page h2 {
  font-size: 0.85rem; font-family: var(--sans); font-weight: 700; letter-spacing: 0.12em; text-transform: uppercase;
  color: var(--accent); margin: 1.6rem 0 0.6rem; padding-bottom: 0.35rem; border-bottom: 1px solid var(--line);
}
.page h2:first-child { margin-top: 0; }
.desc { font-family: var(--serif); font-style: italic; color: var(--ink-2); margin: 0 0 0.8rem; }
.facts-line { display: flex; flex-wrap: wrap; gap: 0.3rem 1.2rem; color: var(--ink-2); font-size: 0.9rem; margin: 0 0 0.8rem; }
.facts-line b { color: var(--ink); font-weight: 600; }
.actions { display: flex; flex-wrap: wrap; gap: 0.45rem; margin: 0.9rem 0 0; }
.actions .btn { font-size: 0.875rem; padding: 0.35em 0.8em; }

.gallery-main { width: 100%; aspect-ratio: 4 / 3; object-fit: cover; border-radius: 4px; background: var(--paper-2); }
.gallery-thumbs { display: flex; gap: 0.4rem; margin: 0.45rem 0 0.9rem; overflow-x: auto; }
.gallery-thumbs button { padding: 0; border: 2px solid transparent; border-radius: 4px; background: none; cursor: pointer; flex: none; }
.gallery-thumbs button[aria-current="true"] { border-color: var(--accent); }
.gallery-thumbs img { width: 3.6rem; height: 3.6rem; object-fit: cover; border-radius: 2px; }

.ings { list-style: none; margin: 0; padding: 0; }
.ing-header { font: 600 1rem var(--serif); margin: 0.9rem 0 0.2rem; }
.ing-row {
  display: flex; align-items: baseline; gap: 0.6rem; width: 100%; text-align: left;
  padding: 0.5rem 0.4rem; border: 0; border-bottom: 1px dotted var(--line); background: none; cursor: pointer; border-radius: 4px;
}
.ing-row:hover { background: var(--paper-2); }
.ing-text { flex: 1; min-width: 0; }
.ing-instead { display: block; font-size: 0.8rem; color: var(--ink-2); text-decoration: line-through; }
.swapped .ing-text > span:first-child { color: var(--accent); font-weight: 600; }
.note-chip { flex: none; font-size: 0.72rem; color: var(--ink-2); border: 1px solid var(--line); border-radius: 999px; padding: 0.05em 0.55em; white-space: nowrap; }
.note-chip.warn { color: var(--med); border-color: currentColor; }
.hint { color: var(--ink-2); font-size: 0.85rem; margin: 0.6rem 0 0; }
.banner {
  display: flex; flex-wrap: wrap; align-items: center; gap: 0.5rem 0.9rem;
  margin: 0.8rem 0 0; padding: 0.6rem 0.8rem; border-radius: 8px; background: var(--paper-2); font-size: 0.9rem;
}
.banner.warn { border-left: 3px solid var(--med); }
main > .banner { margin-top: 1.5rem; background: var(--paper); box-shadow: var(--shadow); }
.progress { flex: 1 1 8rem; height: 6px; border-radius: 3px; background: var(--line); overflow: hidden; }
.progress > i { display: block; height: 100%; background: var(--accent); transition: width 0.3s; }

.steps { margin: 0; padding: 0; list-style: none; counter-reset: step; }
.steps li { counter-increment: step; position: relative; padding: 0 0 1rem 2.6rem; font-family: var(--serif); font-size: 1.06rem; line-height: 1.55; }
.steps li::before {
  content: counter(step); position: absolute; left: 0; top: 0.05rem;
  width: 1.8rem; height: 1.8rem; border-radius: 50%; display: grid; place-items: center;
  font: 700 0.9rem var(--sans); color: var(--accent); border: 1.5px solid var(--accent);
}
.notes { white-space: pre-wrap; font-family: var(--serif); margin: 0; }

/* ---------- Nutrition and glycemic load ---------- */
.facts { display: grid; grid-template-columns: minmax(17rem, 21rem) 1fr; gap: 1.5rem; margin-top: 1.5rem; align-items: start; }
.panel { background: var(--paper); border-radius: 8px; box-shadow: var(--shadow); padding: 1.25rem 1.4rem; }
.nf { border: 2px solid var(--ink); padding: 0.5rem 0.7rem 0.6rem; font-size: 0.92rem; line-height: 1.3; font-variant-numeric: tabular-nums; }
.nf h2 { font: 900 2.15rem/1 var(--sans); letter-spacing: -0.02em; padding-bottom: 0.25rem; border-bottom: 1px solid var(--ink); }
.nf-serving { display: flex; justify-content: space-between; gap: 1rem; padding: 0.2rem 0; }
.nf-serving.strong { font-weight: 700; }
.nf-thick { border-top: 9px solid var(--ink); margin-top: 0.25rem; }
.nf-medium { border-top: 5px solid var(--ink); }
.nf-cal { display: flex; justify-content: space-between; align-items: flex-end; font-weight: 800; }
.nf-cal span:first-child { font-size: 1.5rem; }
.nf-cal span:last-child { font-size: 2.4rem; line-height: 1; }
.nf-small { font-size: 0.78rem; font-weight: 700; }
.nf-dv { text-align: right; font-size: 0.78rem; font-weight: 700; padding: 0.15rem 0; }
.nf-row { display: flex; justify-content: space-between; gap: 1rem; padding: 0.18rem 0; border-top: 1px solid var(--ink-2); }
.nf-row.indent { padding-left: 1rem; }
.nf-foot { font-size: 0.72rem; color: var(--ink-2); margin: 0.4rem 0 0; }

.gl-head { display: flex; flex-wrap: wrap; align-items: baseline; gap: 0.5rem 0.9rem; }
.gl-head h2 { font-size: 1.3rem; }
.gl-number { font: 700 3.2rem/1 var(--serif); font-variant-numeric: tabular-nums; }
.gl-number small { font: 400 0.95rem var(--sans); color: var(--ink-2); margin-left: 0.4rem; }
.gl-band { font-weight: 700; padding: 0.15em 0.75em; border-radius: 999px; color: var(--paper); }
.gl-band.low { background: var(--low); }
.gl-band.medium { background: var(--med); }
.gl-band.high { background: var(--high); }
.gl-scale { position: relative; height: 10px; border-radius: 5px; margin: 1.1rem 0 0.3rem;
  background: linear-gradient(90deg, var(--low) 0 25%, var(--med) 25% 50%, var(--high) 50% 100%); }
.gl-marker { position: absolute; top: -5px; width: 4px; height: 20px; border-radius: 2px; background: var(--ink); transform: translateX(-50%); box-shadow: 0 0 0 2px var(--paper); }
.gl-ticks { display: flex; justify-content: space-between; font-size: 0.75rem; color: var(--ink-2); }
.gl-table { width: 100%; border-collapse: collapse; margin-top: 1rem; font-size: 0.9rem; font-variant-numeric: tabular-nums; }
.gl-table th { text-align: right; font-weight: 600; color: var(--ink-2); font-size: 0.78rem; padding: 0.3rem 0.4rem; border-bottom: 1px solid var(--line); }
.gl-table th:first-child, .gl-table td:first-child { text-align: left; }
.gl-table td { text-align: right; padding: 0.35rem 0.4rem; border-bottom: 1px dotted var(--line); }
.bar { display: inline-block; height: 0.5em; border-radius: 2px; background: var(--accent); margin-right: 0.4em; vertical-align: middle; }

/* ---------- Ingredient sheet ---------- */
dialog.sheet {
  width: min(34rem, calc(100vw - 1.5rem)); max-height: calc(100dvh - 2rem); padding: 0; border: 0; border-radius: 12px;
  background: var(--paper); color: var(--ink); box-shadow: var(--shadow);
}
dialog.sheet::backdrop { background: rgb(20 14 8 / 0.5); }
.sheet-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 1rem; padding: 1.1rem 1.25rem 0.6rem; position: sticky; top: 0; background: var(--paper); }
.sheet-head h2 { font-size: 1.25rem; }
.sheet-body { padding: 0 1.25rem 1.25rem; }
.sheet h3 { font: 700 0.78rem var(--sans); letter-spacing: 0.1em; text-transform: uppercase; color: var(--accent); margin: 1.1rem 0 0.5rem; }
.opt { display: flex; align-items: flex-start; gap: 0.65rem; padding: 0.6rem 0.7rem; border: 1px solid var(--line); border-radius: 8px; margin-bottom: 0.45rem; cursor: pointer; }
.opt.on { border-color: var(--accent); background: var(--paper-2); }
.opt input { margin-top: 0.3rem; accent-color: var(--accent); }
.opt-body { flex: 1; min-width: 0; display: grid; gap: 0.15rem; }
.opt-text { font-weight: 600; }
.opt-text em { font-weight: 400; color: var(--ink-2); }
.opt-note { font-size: 0.85rem; color: var(--ink-2); }
.deltas { display: flex; flex-wrap: wrap; gap: 0.35rem; margin-top: 0.15rem; }
.delta { font-size: 0.75rem; padding: 0.05em 0.55em; border-radius: 999px; border: 1px solid currentColor; font-variant-numeric: tabular-nums; }
.delta.down { color: var(--low); }
.delta.up { color: var(--high); }
.delta.flat { color: var(--ink-2); }
.row { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; }
.row > input { flex: 1 1 10rem; }
.field { display: grid; grid-template-columns: 8.5rem 1fr; gap: 0.3rem 0.75rem; align-items: center; margin-bottom: 0.6rem; }
.field input[type="number"] { width: 6.5rem; }
.field small { grid-column: 2; color: var(--ink-2); font-size: 0.8rem; }
.results { list-style: none; margin: 0.5rem 0 0; padding: 0; max-height: 14rem; overflow: auto; border: 1px solid var(--line); border-radius: 8px; }
.results button { display: flex; justify-content: space-between; gap: 1rem; width: 100%; text-align: left; padding: 0.45rem 0.7rem; border: 0; border-bottom: 1px solid var(--line); background: none; cursor: pointer; font-size: 0.9rem; }
.results button:hover { background: var(--paper-2); }
.results span:last-child { color: var(--ink-2); white-space: nowrap; }

/* ---------- Editor ---------- */
.editor { max-width: 46rem; }
.editor .panel { margin-bottom: 1.25rem; }
.editor h1 { font-size: 1.9rem; margin: 0.4rem 0 1rem; }
.editor label { display: block; font-weight: 600; margin: 1rem 0 0.3rem; }
.editor label small { font-weight: 400; color: var(--ink-2); margin-left: 0.4em; }
.editor label:first-child { margin-top: 0; }
.trio { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0.75rem; }
.import h2 { font-size: 1.15rem; margin-bottom: 0.2rem; }
.import p { margin: 0 0 0.8rem; color: var(--ink-2); font-size: 0.9rem; }
.import details { margin-top: 0.8rem; }
.import summary { cursor: pointer; color: var(--accent); }
.import textarea { margin: 0.6rem 0; min-height: 9rem; }
.photo-strip { display: flex; flex-wrap: wrap; gap: 0.6rem; }
.thumb { position: relative; margin: 0; width: 7rem; }
.thumb img { width: 7rem; height: 7rem; object-fit: cover; border-radius: 6px; }
.thumb figcaption, .thumb .link { display: block; font-size: 0.78rem; text-align: center; margin: 0.2rem auto 0; color: var(--ink-2); }
.thumb .link { color: var(--accent); }
.thumb .remove { position: absolute; top: 4px; right: 4px; background: rgb(0 0 0 / 0.6); color: #fff; border-radius: 50%; width: 1.5rem; height: 1.5rem; padding: 0; }
.add-photo {
  width: 7rem; height: 7rem; display: grid; place-items: center; text-align: center; margin: 0 !important;
  border: 2px dashed var(--line); border-radius: 6px; color: var(--ink-2); cursor: pointer; font-weight: 400 !important; font-size: 0.85rem;
}
.add-photo:hover, .photo-strip.drag .add-photo { border-color: var(--accent); color: var(--accent); }
.form-actions { display: flex; gap: 0.6rem; justify-content: flex-end; position: sticky; bottom: 0; padding: 0.8rem 0; background: linear-gradient(transparent, var(--desk) 35%); }

/* ---------- Toast ---------- */
#toast {
  position: fixed; left: 50%; bottom: 1.5rem; transform: translate(-50%, 1rem); z-index: 100;
  max-width: min(32rem, calc(100vw - 2rem)); padding: 0.65rem 1rem; border-radius: 8px;
  background: var(--ink); color: var(--paper); box-shadow: var(--shadow);
  opacity: 0; pointer-events: none; transition: opacity 0.2s, transform 0.2s;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.error { background: var(--high); color: #fff; }

@media (max-width: 820px) {
  .book { grid-template-columns: 1fr; }
  .book::after { display: none; }
  .page + .page { padding-top: 0; }
  .facts { grid-template-columns: 1fr; }
  .trio { grid-template-columns: 1fr 1fr; }
  .field { grid-template-columns: 1fr; }
  .field small { grid-column: 1; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
@media print {
  body { background: #fff; }
  .topbar, .actions, .note-chip, #toast { display: none; }
  .book, .panel { box-shadow: none; }
}
