:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface-2: #f0ede6;
  --ink: #21241f;
  --ink-soft: #6b6f66;
  --line: #e4e0d7;
  --brand: #1f7a53;
  --brand-soft: #e2f0e8;
  --accent: #e8703a;
  --danger: #c0432f;
  --shadow: 0 1px 2px rgba(20, 30, 24, .06), 0 8px 24px rgba(20, 30, 24, .08);
  --radius: 16px;
  --radius-sm: 10px;
  --tap: 48px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bot: env(safe-area-inset-bottom, 0px);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  overscroll-behavior-y: none;
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; cursor: pointer; border: none; background: none; }
input { font: inherit; }
img { display: block; }

#app { min-height: 100dvh; display: flex; flex-direction: column; }

/* ---------- Topbar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  padding: calc(var(--safe-top) + 14px) 18px 12px;
  display: flex; align-items: center; justify-content: space-between;
  background: color-mix(in srgb, var(--bg) 86%, transparent);
  backdrop-filter: saturate(1.4) blur(12px);
  -webkit-backdrop-filter: saturate(1.4) blur(12px);
  border-bottom: 1px solid var(--line);
}
.topbar h1 { margin: 0; font-size: 22px; font-weight: 800; letter-spacing: -.02em; }
.sync-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--ink-soft); transition: background .3s; }
.sync-dot.ok { background: var(--brand); }
.sync-dot.local { background: var(--accent); }
.sync-dot.err { background: var(--danger); }
.sync-dot.busy { background: #d6b64a; animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .35; } }

/* ---------- Layout ---------- */
#main { flex: 1; padding-bottom: calc(72px + var(--safe-bot)); }
.view { padding: 14px 14px 24px; max-width: 720px; margin: 0 auto; }
.view[hidden] { display: none; }

/* ---------- Search ---------- */
.search {
  position: relative; margin: 4px 0 16px;
}
.search input {
  width: 100%; height: var(--tap);
  padding: 0 16px 0 44px;
  border-radius: 999px; border: 1px solid var(--line);
  background: var(--surface); color: var(--ink);
  outline: none; font-size: 16px;
}
.search input:focus { border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
.search::before {
  content: "🔍"; position: absolute; left: 16px; top: 50%; transform: translateY(-50%);
  font-size: 15px; opacity: .6;
}

/* ---------- Recipe grid ---------- */
.grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
@media (min-width: 560px) { .grid { grid-template-columns: repeat(3, 1fr); } }

.card {
  background: var(--surface); border-radius: var(--radius); overflow: hidden;
  box-shadow: var(--shadow); text-align: left; display: flex; flex-direction: column;
  transition: transform .12s ease;
}
.card:active { transform: scale(.97); }
.card-img { aspect-ratio: 1 / 1; width: 100%; object-fit: cover; background: var(--surface-2); }
.card-body { padding: 10px 11px 12px; display: flex; flex-direction: column; gap: 6px; flex: 1; }
.card-title { font-size: 14px; font-weight: 700; line-height: 1.25; letter-spacing: -.01em; }
.card-meta { margin-top: auto; display: flex; gap: 10px; font-size: 12px; color: var(--ink-soft); }
.card-meta span { display: inline-flex; align-items: center; gap: 3px; }

.empty { text-align: center; color: var(--ink-soft); padding: 48px 20px; font-size: 15px; }

/* Recipe filter segmented control */
.segment { display: flex; gap: 6px; background: var(--surface-2); border-radius: 999px; padding: 4px; margin: 0 0 16px; }
.seg-btn { flex: 1; height: 40px; border-radius: 999px; font-size: 14px; font-weight: 700; color: var(--ink-soft); transition: all .12s; }
.seg-btn.is-active { background: var(--surface); color: var(--brand); box-shadow: var(--shadow); }

/* "Miki tycker om" toggle inside a recipe */
.like-toggle { display: inline-flex; align-items: center; gap: 8px; margin-top: 14px; min-height: 44px;
  padding: 8px 16px; border-radius: 999px; border: 1.5px solid var(--line); background: var(--surface);
  color: var(--ink-soft); font-weight: 700; font-size: 15px; transition: all .12s; }
.like-toggle.on { border-color: var(--brand); background: var(--brand-soft); color: var(--brand); }
.like-toggle:active { transform: scale(.97); }
.like-heart { font-size: 18px; line-height: 1; }

/* "Köpa färdigt" (ready-made) items */
.buy-tag { display: inline-flex; align-items: center; gap: 3px; background: var(--accent); color: #fff;
  font-size: 11px; font-weight: 800; padding: 3px 9px; border-radius: 999px; }
.buy-note { margin-top: 14px; padding: 12px 14px; border-radius: 12px; background: var(--brand-soft);
  display: flex; flex-direction: column; gap: 2px; }
.buy-note strong { font-size: 15px; }
.buy-note span { color: var(--ink-soft); font-size: 14px; }

/* ---------- Recipe sheet ---------- */
.sheet { position: fixed; inset: 0; z-index: 60; }
.sheet[hidden] { display: none; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(20,24,20,.42); }
.sheet-panel {
  position: absolute; left: 0; right: 0; bottom: 0; top: 8%;
  background: var(--bg); border-radius: 22px 22px 0 0; overflow-y: auto;
  box-shadow: 0 -8px 40px rgba(0,0,0,.28);
  animation: slideup .28s cubic-bezier(.2,.7,.3,1);
  padding-bottom: calc(24px + var(--safe-bot));
}
@keyframes slideup { from { transform: translateY(100%); } }
.sheet-hero { position: relative; }
.sheet-hero img { width: 100%; aspect-ratio: 16/10; object-fit: cover; background: var(--surface-2); }
.sheet-grip { position: absolute; top: 8px; left: 50%; transform: translateX(-50%); width: 40px; height: 5px; border-radius: 3px; background: rgba(255,255,255,.85); }
.sheet-close {
  position: absolute; top: 12px; right: 12px; width: 38px; height: 38px; border-radius: 50%;
  background: rgba(0,0,0,.5); color: #fff; font-size: 20px; display: grid; place-items: center;
}
.sheet-body { padding: 18px 18px 8px; }
.sheet-body h2 { margin: 0 0 6px; font-size: 23px; font-weight: 800; letter-spacing: -.02em; }
.pill-row { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0 4px; }
.pill { background: var(--surface); border: 1px solid var(--line); border-radius: 999px; padding: 6px 12px; font-size: 13px; color: var(--ink-soft); font-weight: 600; }
.pill strong { color: var(--ink); }
.section-h { margin: 22px 0 10px; font-size: 13px; font-weight: 800; text-transform: uppercase; letter-spacing: .08em; color: var(--brand); }
.ing-list { list-style: none; margin: 0; padding: 0; }
.ing-list li { padding: 11px 2px; border-bottom: 1px solid var(--line); font-size: 15px; display: flex; gap: 10px; }
.ing-list li::before { content: "•"; color: var(--brand); font-weight: 800; }
.steps { margin: 0; padding: 0; list-style: none; counter-reset: s; }
.steps li { counter-increment: s; position: relative; padding: 12px 0 12px 40px; border-bottom: 1px solid var(--line); font-size: 15px; line-height: 1.5; }
.steps li::before { content: counter(s); position: absolute; left: 0; top: 11px; width: 26px; height: 26px; border-radius: 50%; background: var(--brand-soft); color: var(--brand); font-weight: 800; font-size: 13px; display: grid; place-items: center; }
.src { margin-top: 16px; font-size: 13px; color: var(--ink-soft); }
.src a { color: var(--brand); }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: var(--tap); padding: 0 18px; border-radius: 12px; font-weight: 700; font-size: 16px;
}
.btn-primary { background: var(--brand); color: #fff; box-shadow: var(--shadow); }
.btn-primary:active { background: #1a6746; }
.btn-block { display: flex; width: 100%; }
.btn-ghost { background: var(--surface); border: 1px solid var(--line); color: var(--ink); }
.btn-sticky { position: sticky; bottom: calc(12px + var(--safe-bot)); margin: 18px 18px 0; }

/* ---------- Shopping ---------- */
.shop-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 6px; }
.shop-sub { color: var(--ink-soft); font-size: 13px; margin: 0 2px 14px; }
.zone { margin-bottom: 18px; }
.zone-h { font-size: 13px; font-weight: 800; text-transform: uppercase; letter-spacing: .07em; color: var(--ink-soft); margin: 0 4px 8px; display: flex; justify-content: space-between; }
.rows { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.row { display: flex; align-items: center; gap: 12px; padding: 0 14px; min-height: var(--tap); border-bottom: 1px solid var(--line); }
.row:last-child { border-bottom: none; }
.check { width: 26px; height: 26px; border-radius: 8px; border: 2px solid var(--line); flex: none; display: grid; place-items: center; color: #fff; font-size: 15px; transition: all .12s; }
.row.done .check { background: var(--brand); border-color: var(--brand); }
.row-name { flex: 1; font-size: 16px; padding: 11px 0; }
.row.done .row-name { text-decoration: line-through; color: var(--ink-soft); }
.row-tag { font-size: 11px; color: var(--accent); font-weight: 700; }
.row-del { width: 34px; height: 34px; border-radius: 8px; display: grid; place-items: center; color: var(--ink-soft); font-size: 18px; flex: none; }

.addbar { display: flex; gap: 8px; margin: 4px 0 18px; }
.addbar input { flex: 1; height: var(--tap); padding: 0 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--surface); outline: none; font-size: 16px; }
.addbar input:focus { border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
.addbar button { flex: none; width: var(--tap); height: var(--tap); border-radius: 12px; background: var(--brand); color: #fff; font-size: 24px; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 0 0 8px; }
.chip { background: var(--surface); border: 1px solid var(--line); border-radius: 999px; padding: 8px 13px; font-size: 14px; font-weight: 600; }
.chip:active { background: var(--brand-soft); border-color: var(--brand); }

.linkbtn { color: var(--brand); font-weight: 700; font-size: 14px; padding: 8px; }
.linkbtn.danger { color: var(--danger); }

/* ---------- Master ---------- */
.master-zone { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); margin-bottom: 14px; overflow: hidden; }
.master-zone > summary { padding: 14px 16px; font-weight: 800; font-size: 16px; list-style: none; display: flex; justify-content: space-between; align-items: center; cursor: pointer; }
.master-zone > summary::-webkit-details-marker { display: none; }
.master-zone[open] > summary { border-bottom: 1px solid var(--line); }
.master-items { padding: 6px 16px 14px; display: flex; flex-wrap: wrap; gap: 8px; }
.mi { background: var(--surface-2); border-radius: 999px; padding: 7px 12px; font-size: 14px; display: inline-flex; align-items: center; gap: 8px; }
.mi button { color: var(--ink-soft); font-size: 15px; line-height: 1; }

/* ---------- Tabbar ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: flex; padding: 6px 8px calc(6px + var(--safe-bot));
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(14px) saturate(1.3); -webkit-backdrop-filter: blur(14px) saturate(1.3);
  border-top: 1px solid var(--line);
}
.tab {
  flex: 1; position: relative; display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 8px 0; color: var(--ink-soft); font-size: 11px; font-weight: 700; border-radius: 12px;
}
.tab-ico { font-size: 22px; filter: grayscale(.4) opacity(.7); transition: all .15s; }
.tab.is-active { color: var(--brand); }
.tab.is-active .tab-ico { filter: none; transform: translateY(-1px); }
.badge {
  position: absolute; top: 2px; right: 50%; margin-right: -26px;
  min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px;
  background: var(--accent); color: #fff; font-size: 11px; font-weight: 800;
  display: grid; place-items: center;
}

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(84px + var(--safe-bot)); transform: translateX(-50%);
  background: var(--ink); color: #fff; padding: 12px 18px; border-radius: 999px;
  font-size: 14px; font-weight: 600; z-index: 80; box-shadow: var(--shadow);
  animation: toastin .2s ease;
}
.toast[hidden] { display: none; }
@keyframes toastin { from { opacity: 0; transform: translate(-50%, 8px); } }

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161815; --surface: #20231f; --surface-2: #2a2e28;
    --ink: #ecefe8; --ink-soft: #9aa093; --line: #333831;
    --brand: #46b483; --brand-soft: #23352c; --accent: #f08a55;
  }
}
