:root {
  --bg: #ffffff;
  --surface: #f6f6f4;
  --text: #1c1c1c;
  --muted: #6b6b6b;
  --border: #e2e2de;
  --accent: #2f7d5b;
  --accent-text: #ffffff;
  --accent-soft: #e6f2ec;
  --danger: #b3261e;
  --danger-soft: #fbeceb;
  --focus: #2f7d5b66;
  --radius: 8px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #141414;
    --surface: #1e1e1e;
    --text: #ececec;
    --muted: #9a9a9a;
    --border: #2f2f2f;
    --accent: #5cbf91;
    --accent-text: #0d1f17;
    --accent-soft: #1b2c24;
    --danger: #f2827b;
    --danger-soft: #2d1a19;
    --focus: #5cbf9166;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #141414;
  --surface: #1e1e1e;
  --text: #ececec;
  --muted: #9a9a9a;
  --border: #2f2f2f;
  --accent: #5cbf91;
  --accent-text: #0d1f17;
  --accent-soft: #1b2c24;
  --danger: #f2827b;
  --danger-soft: #2d1a19;
  --focus: #5cbf9166;
  color-scheme: dark;
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Arial, Helvetica, sans-serif;
  font-size: 16px;
  line-height: 1.5;
}
a { color: var(--accent); }
h1 { font-size: 1.5rem; margin: 0 0 .5rem; line-height: 1.25; }
h2 { font-size: 1.1rem; margin: 0 0 .75rem; }
code { font-family: inherit; font-size: .95em; background: var(--surface); padding: .1em .35em; border-radius: 4px; word-break: break-all; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
[hidden] { display: none !important; }

/* Layout */
.top {
  display: flex; align-items: center; gap: 16px;
  max-width: 960px; margin: 0 auto; padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: bold; color: var(--text); text-decoration: none; }
.top nav { display: flex; gap: 4px; margin-left: auto; }
.top nav a {
  color: var(--muted); text-decoration: none; padding: 8px 10px; border-radius: var(--radius);
  min-height: 44px; display: inline-flex; align-items: center;
}
.top nav a[aria-current="page"] { color: var(--text); background: var(--surface); }
.top .icon-btn { margin-left: auto; }
.top nav ~ .icon-btn { margin-left: 0; }
main { max-width: 960px; margin: 0 auto; padding: 20px 16px 120px; }
footer { max-width: 960px; margin: 0 auto; padding: 0 16px 32px; }
.narrow { max-width: 420px; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.page-head h1 { margin: 0; }

/* Forms */
label { display: block; font-size: .875rem; color: var(--muted); }
label > input, label > select, label > textarea { margin-top: 4px; }
input, select, textarea {
  width: 100%; font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: var(--radius);
  padding: 10px 12px; min-height: 44px;
}
input[type="checkbox"] { width: 22px; height: 22px; min-height: 0; accent-color: var(--accent); margin: 0; }
input[type="file"] { padding: 6px; }
input[type="file"]::file-selector-button {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 6px; padding: 6px 12px; margin-right: 10px; cursor: pointer;
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus, button:focus-visible, a:focus-visible {
  outline: 3px solid var(--focus); outline-offset: 1px;
}
.stack { display: grid; gap: 14px; }
.row-2 { display: flex; gap: 12px; align-items: flex-end; flex-wrap: wrap; }
.grow { flex: 1 1 200px; min-width: 0; }
.servings { width: 110px; }
.inline { display: flex; align-items: center; gap: 8px; }
.inline select { width: auto; margin: 0; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 10px 16px; border-radius: var(--radius);
  border: 1px solid var(--border); background: var(--bg); color: var(--text);
  font: inherit; text-decoration: none; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--surface); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: bold; }
.btn-primary:hover { background: var(--accent); filter: brightness(1.08); }
.btn-danger { color: var(--danger); }
.btn:disabled { opacity: .6; cursor: progress; }
.btn.busy::before {
  content: ""; width: 14px; height: 14px; border-radius: 50%;
  border: 2px solid currentColor; border-right-color: transparent; animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 44px; height: 44px; border: 0; border-radius: var(--radius);
  background: transparent; color: var(--muted); font-size: 1.25rem; cursor: pointer;
}
.icon-btn:hover { background: var(--surface); color: var(--text); }
.link-btn { border: 0; background: none; color: var(--muted); font: inherit; padding: 8px 0; cursor: pointer; text-decoration: underline; }
.actions { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 20px; }
.actions form { margin: 0; }

.notice { padding: 10px 14px; border-radius: var(--radius); background: var(--accent-soft); margin: 0 0 16px; }
.notice-error { background: var(--danger-soft); color: var(--danger); }
ul.notice { padding-left: 32px; }

.divider { display: flex; align-items: center; gap: 12px; color: var(--muted); margin: 24px 0; font-size: .875rem; }
.divider::before, .divider::after { content: ""; flex: 1; border-top: 1px solid var(--border); }

/* Library */
.search { margin-bottom: 14px; }
.cards { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
@media (min-width: 720px) { .cards { grid-template-columns: 1fr 1fr; } }
.card {
  display: flex; align-items: center; gap: 12px;
  border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 12px;
}
.card.selected { border-color: var(--accent); background: var(--accent-soft); }
.pick { display: flex; align-items: center; justify-content: center; min-width: 32px; min-height: 44px; cursor: pointer; }
.card-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.card-title { color: var(--text); text-decoration: none; font-weight: bold; overflow-wrap: anywhere; }
.card-title:hover { text-decoration: underline; }
.card-main .muted { font-size: .8125rem; }

.stepper { display: inline-flex; align-items: center; border: 1px solid var(--border); border-radius: var(--radius); }
.stepper button {
  width: 40px; height: 42px; border: 0; background: none; color: var(--text); font-size: 1.2rem; cursor: pointer;
}
.stepper button:hover { background: var(--surface); }
.stepper input {
  width: 44px; min-height: 42px; border: 0; border-radius: 0; text-align: center; padding: 0; margin: 0;
  -moz-appearance: textfield; appearance: textfield;
}
.stepper input::-webkit-outer-spin-button, .stepper input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }

.action-bar {
  position: sticky; bottom: 0; z-index: 2;
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  margin: 16px -16px 0; padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: var(--bg); border-top: 1px solid var(--border);
}
.action-bar .btn-primary { flex: 0 0 auto; }
@media (max-width: 480px) { .action-bar .btn-primary { flex: 1 1 100%; } }

.empty { text-align: center; padding: 48px 0; }

/* Recipe view */
.plates-form { display: flex; align-items: center; gap: 12px; margin: 16px 0; }
.ing-list { list-style: none; padding: 0; margin: 0; border-top: 1px solid var(--border); }
.ing-list li { display: flex; gap: 16px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.ing-list .qty { flex: 0 0 90px; text-align: right; font-weight: bold; white-space: nowrap; }
.notes { white-space: pre-line; margin-top: 16px; }

/* Recipe form */
.recipe-form { display: grid; gap: 14px; }
.ing-head { display: none; }
.ing-rows { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.ing-row {
  display: grid; gap: 6px;
  grid-template-columns: 1fr 1fr 44px;
  grid-template-areas: "name name rm" "qty unit ." "note cat .";
  padding: 10px; border: 1px solid var(--border); border-radius: var(--radius);
}
.ing-row [name="name"] { grid-area: name; }
.ing-row [name="qty"] { grid-area: qty; }
.ing-row [name="unit"] { grid-area: unit; }
.ing-row [name="note"] { grid-area: note; }
.ing-row [name="category"] { grid-area: cat; }
.ing-row [data-remove-row] { grid-area: rm; }
@media (min-width: 760px) {
  .ing-head, .ing-row { grid-template-columns: 2fr 90px 90px 1.5fr 140px 44px; grid-template-areas: "name qty unit note cat rm"; }
  .ing-head { display: grid; gap: 6px; font-size: .8125rem; color: var(--muted); padding: 0 1px; margin-bottom: -8px; }
  .ing-row { padding: 0; border: 0; }
}

/* Shopping preview */
.shop-list { list-style: none; padding: 0; margin: 0; }
.shop-list li { display: flex; align-items: center; gap: 12px; padding: 6px 0; border-bottom: 1px solid var(--border); }
.shop-list li input[name^="title"] { border-color: transparent; padding-left: 4px; }
.shop-list li input[name^="title"]:focus { border-color: var(--border); }
.shop-list li .muted { padding-left: 5px; }
.shop-list li:has(input[type="checkbox"]:not(:checked)) .grow { opacity: .45; text-decoration: line-through; }

.result-list { list-style: none; padding: 0; }
.result-list li { display: flex; gap: 10px; padding: 6px 0; border-bottom: 1px solid var(--border); }
.result-list .ok > span:first-child { color: var(--accent); }
.result-list .failed > span:first-child { color: var(--danger); }

.panel { border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; margin-bottom: 16px; }
.panel p:first-of-type { margin-top: 0; }
