:root {
  --bg: #fbf7f4;
  --surface: #ffffff;
  --surface-2: #f3ece6;
  --text: #2b2421;
  --muted: #7a6e68;
  --line: #e7ddd5;
  --accent: #e8590c;
  --accent-text: #ffffff;
  --accent-soft: #fde7da;
  --danger: #c92a2a;
  --ok: #2f9e44;
  --radius: 14px;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 4px 16px rgb(0 0 0 / 6%);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #181412;
    --surface: #221d1a;
    --surface-2: #2c2622;
    --text: #f3ece6;
    --muted: #a79a92;
    --line: #3a322d;
    --accent: #ff7a33;
    --accent-text: #1a0d05;
    --accent-soft: #3d2416;
    --danger: #ff6b6b;
    --ok: #69db7c;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

h1, h2, h3 { margin: 0; line-height: 1.2; }
.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 1.4em; margin: 0; }

/* --- boutons et champs --- */
.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  border-radius: 999px;
  padding: 10px 16px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.danger-text { border: none; background: none; color: var(--danger); }
.btn.wide { width: 100%; }
.btn:disabled { opacity: .5; cursor: default; }
.btn:focus-visible, input:focus-visible, .tab:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

input {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 12px;
  min-height: 44px;
  width: 100%;
}

.link-btn {
  border: none; background: none; color: var(--muted);
  font: inherit; font-size: 14px; font-weight: 600; min-height: 44px; padding: 0 4px; cursor: pointer;
}

/* --- connexion --- */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: min(360px, 100%);
  background: var(--surface);
  border-radius: 20px;
  padding: 28px 22px;
  box-shadow: var(--shadow);
  display: grid;
  gap: 10px;
}
.login-card img { border-radius: 14px; }
.login-card label { font-weight: 600; margin-top: 8px; }

/* --- structure --- */
#app { max-width: 720px; margin: 0 auto; padding-bottom: calc(72px + env(safe-area-inset-bottom)); }
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 8px;
  padding: calc(12px + env(safe-area-inset-top)) 16px 12px;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(8px);
}
.topbar h1 { font-size: 24px; flex: 1; }
.sync { color: var(--ok); font-size: 12px; transition: color .3s; }
.sync.stale { color: var(--muted); }
.sync.error { color: var(--danger); }
.view { padding: 0 16px 16px; }

.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 5;
  display: flex; justify-content: center; gap: 8px;
  padding: 8px 16px calc(8px + env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--line);
}
.tab {
  flex: 0 1 180px;
  border: none; background: none; color: var(--muted);
  font: inherit; font-size: 20px;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 6px 12px; border-radius: 14px; cursor: pointer;
}
.tab span { font-size: 13px; font-weight: 600; }
.tab.active { background: var(--accent-soft); color: var(--text); }

/* --- liste de courses --- */
.add-form { display: flex; gap: 8px; margin: 4px 0 12px; }
.add-form .qty { width: 92px; flex: none; }
.add-form .btn { width: 44px; padding: 0; font-size: 22px; flex: none; }
.summary { margin: 0 0 8px; color: var(--muted); font-size: 14px; }

.items { list-style: none; margin: 0; padding: 0; background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); }
.items:empty { display: none; }
.item {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 8px 12px 14px;
  border-bottom: 1px solid var(--line);
  cursor: pointer; user-select: none;
}
.item:last-child { border-bottom: none; }
.check {
  flex: none; width: 26px; height: 26px; border-radius: 8px;
  border: 2px solid var(--muted);
  display: grid; place-items: center; color: var(--accent-text); font-size: 16px; font-weight: 700;
}
.item.checked .check { background: var(--accent); border-color: var(--accent); }
.item-main { flex: 1; min-width: 0; }
.item-name { font-weight: 600; overflow-wrap: anywhere; }
.item-sources { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item-qty { flex: none; max-width: 45%; text-align: right; font-weight: 600; font-variant-numeric: tabular-nums; font-size: 15px; }
.item.checked .item-name, .item.checked .item-qty { text-decoration: line-through; color: var(--muted); }
.item-delete { flex: none; border: none; background: none; color: var(--muted); width: 36px; height: 36px; border-radius: 50%; cursor: pointer; font-size: 18px; }

.done { margin-top: 16px; }
.done summary { cursor: pointer; color: var(--muted); font-weight: 600; padding: 8px 2px; }
.row-actions { display: flex; justify-content: center; padding: 12px 0; }
.row-actions.bottom { padding-top: 20px; }

.empty { text-align: center; padding: 40px 12px; }
.empty p { margin: 4px 0; }

/* --- recettes --- */
.hint { margin: 0 0 12px; font-size: 14px; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(156px, 1fr)); gap: 12px; padding-bottom: 72px; }
.card {
  background: var(--surface); border-radius: var(--radius); overflow: hidden;
  box-shadow: var(--shadow); position: relative; cursor: pointer;
  border: 2px solid transparent;
}
.card.selected { border-color: var(--accent); }
.card-img { aspect-ratio: 1; width: 100%; object-fit: cover; display: block; background: var(--surface-2); }
.card-body { padding: 10px 12px 12px; }
.card-title { font-weight: 700; font-size: 15px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.card-meta { font-size: 13px; color: var(--muted); margin-top: 2px; }
.card-select {
  position: absolute; top: 8px; right: 8px;
  width: 40px; height: 40px; border-radius: 50%;
  border: none; background: color-mix(in srgb, var(--surface) 88%, transparent);
  display: grid; place-items: center; cursor: pointer;
}
.card-select .check { width: 24px; height: 24px; }
.card.selected .card-select .check { background: var(--accent); border-color: var(--accent); }

.selection-bar {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(80px + env(safe-area-inset-bottom)); z-index: 6;
  width: min(688px, calc(100% - 24px));
  display: grid; grid-template-columns: 1fr 1fr; gap: 8px;
  background: var(--surface); border-radius: 18px; padding: 10px 12px 12px;
  box-shadow: 0 6px 24px rgb(0 0 0 / 18%); border: 1px solid var(--line);
}
.selection-bar span { grid-column: 1 / -1; font-weight: 600; font-size: 14px; color: var(--muted); }

/* --- détail recette --- */
dialog {
  border: none; padding: 0; background: var(--surface); color: var(--text);
  width: min(640px, 100%); max-height: 100dvh; border-radius: 20px;
}
dialog::backdrop { background: rgb(0 0 0 / 45%); }
@media (max-width: 640px) {
  dialog { margin: auto 0 0; max-width: 100%; border-radius: 20px 20px 0 0; max-height: 92dvh; }
}
.sheet img { width: 100%; aspect-ratio: 4 / 3; object-fit: cover; display: block; }
.sheet-body { padding: 18px 18px calc(18px + env(safe-area-inset-bottom)); }
.sheet-body h2 { font-size: 22px; }
.sheet-body h3 { margin: 18px 0 6px; font-size: 17px; }
.sheet-body ul, .sheet-body ol { padding-left: 22px; margin: 0; }
.sheet-body li { margin: 4px 0; }
.sheet-body form { margin-top: 20px; }
.verify { color: var(--accent); font-style: italic; }

.toast {
  position: fixed; left: 50%; bottom: calc(150px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(20px);
  background: var(--text); color: var(--bg);
  padding: 10px 16px; border-radius: 12px; font-weight: 600;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 20;
  max-width: calc(100% - 32px);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
