:root {
  --bg: #17161a;
  --bg-soft: #1f1e24;
  --card: #242228;
  --card-2: #2b2931;
  --border: #34323b;
  --text: #eceaf0;
  --text-dim: #a9a6b2;
  --accent: #e9643a;
  --accent-soft: rgba(233, 100, 58, .16);
  --ok: #57b26e;
  --danger: #e25c5c;
  --radius: 16px;
  --shadow: 0 8px 24px rgba(0, 0, 0, .35);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Inter", sans-serif;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; font-size: 15.5px; }
@media (max-width: 480px) { html { font-size: 14px; } }
@media (min-width: 1000px) { html { font-size: 18px; } }

body {
  margin: 0;
  background: radial-gradient(1200px 500px at 80% -10%, #2a221a 0%, var(--bg) 55%) fixed;
  color: var(--text);
  font-family: var(--font);
  line-height: 1.5;
  min-height: 100vh;
  padding-bottom: env(safe-area-inset-bottom);
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 10px 16px;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}
.brand {
  display: flex; align-items: center; gap: 8px;
  font-weight: 800; font-size: 1.05rem; color: var(--text);
}
.brand:hover { text-decoration: none; }
.brand-mark {
  display: grid; place-items: center;
  width: 32px; height: 32px; border-radius: 10px;
  background: var(--accent-soft); color: var(--accent);
  font-size: 1.1rem;
}
.nav { margin-left: auto; display: flex; gap: 6px; }
.nav a {
  padding: 7px 12px; border-radius: 999px;
  color: var(--text-dim); font-weight: 600; font-size: .92rem;
}
.nav a.active { background: var(--accent-soft); color: var(--accent); }
.nav a:hover { text-decoration: none; color: var(--text); }

.add-btn { margin-left: 4px; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 0; cursor: pointer;
  padding: 10px 16px; border-radius: 12px;
  font-family: inherit; font-size: .95rem; font-weight: 700;
  transition: transform .06s ease, filter .15s ease;
  -webkit-tap-highlight-color: transparent;
}
.btn:active { transform: scale(.97); }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { filter: brightness(1.08); }
.btn-ghost { background: transparent; color: var(--text); border: 1px solid var(--border); }
.btn-ghost:hover { background: var(--card-2); }
.btn-danger { background: rgba(226, 92, 92, .12); color: var(--danger); border: 1px solid rgba(226, 92, 92, .3); }
.btn-danger:hover { background: rgba(226, 92, 92, .2); }
.btn-sm { padding: 6px 12px; font-size: .85rem; border-radius: 10px; }

/* ---------- Layout ---------- */
.wrap { max-width: 980px; margin: 0 auto; padding: 18px 16px 90px; }

.hero h1 { margin: 4px 0 14px; font-size: clamp(1.5rem, 4vw, 2rem); }
.hero .sub { color: var(--text-dim); margin: -6px 0 16px; }

.searchbar { position: relative; max-width: 560px; }
.search-icon {
  position: absolute; left: 14px; top: 50%; transform: translateY(-50%);
  width: 20px; height: 20px; color: var(--text-dim);
}
#search, .searchbar input {
  width: 100%; padding: 13px 16px 13px 44px;
  background: var(--card); color: var(--text);
  border: 1px solid var(--border); border-radius: 14px;
  font-family: inherit; font-size: 1rem; outline: none;
}
#search:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

/* ---------- Category chips ---------- */
.chips {
  display: flex; flex-wrap: wrap; gap: 8px; margin: 16px 0 6px;
}
.chip {
  padding: 7px 14px; border-radius: 999px;
  background: var(--card); border: 1px solid var(--border);
  color: var(--text-dim); font-size: .88rem; font-weight: 600;
  cursor: pointer; user-select: none;
  -webkit-tap-highlight-color: transparent;
  transition: all .15s ease;
}
.chip:hover { color: var(--text); border-color: #4a4752; }
.chip.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.chip-tag { cursor: default; background: var(--accent-soft); color: var(--accent); }

/* ---------- List toolbar ---------- */
.list-toolbar {
  display: flex; align-items: center; justify-content: space-between;
  margin: 18px 0 12px; gap: 10px;
}
.sort-wrap { display: flex; align-items: center; gap: 8px; color: var(--text-dim); font-size: .9rem; }
#sort {
  background: var(--card); color: var(--text);
  border: 1px solid var(--border); border-radius: 10px;
  padding: 8px 10px; font-family: inherit; font-size: .9rem; outline: none;
}
.count { color: var(--text-dim); font-size: .9rem; }

/* ---------- Recipe cards ---------- */
.cards {
  display: grid; gap: 14px;
  grid-template-columns: 1fr;
}
@media (min-width: 700px) {
  .cards { grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); }
}
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.rcard {
  display: block; padding: 16px 18px; color: var(--text);
  text-decoration: none; position: relative;
  transition: transform .1s ease, border-color .15s ease;
}
a.rcard:hover { text-decoration: none; border-color: #4a4752; transform: translateY(-1px); }
.rcard h2 { margin: 8px 0 4px; font-size: 1.08rem; line-height: 1.3; }
.rcard .meta {
  display: flex; flex-wrap: wrap; gap: 4px 12px;
  color: var(--text-dim); font-size: .85rem; margin-top: 4px;
}
.rcard .badge {
  position: absolute; top: 14px; right: 14px;
  display: grid; place-items: center;
  width: 40px; height: 40px; border-radius: 12px;
  background: var(--card-2); border: 1px solid var(--border);
  font-size: 1.15rem; cursor: pointer; color: var(--text-dim);
  transition: all .15s ease;
}
.rcard .badge:hover { background: var(--accent); color: #fff; border-color: var(--accent); }
.rcard .badge.done { background: var(--ok); color: #fff; border-color: var(--ok); }

/* ---------- Detail ---------- */
.crumbs { margin: 4px 0 12px; color: var(--text-dim); font-size: .9rem; }
.crumbs a:hover { text-decoration: none; }
.detail { padding: 22px; }
.detail-head h1 { margin: 8px 0 4px; font-size: clamp(1.4rem, 5vw, 2rem); }
.detail-meta { display: flex; flex-wrap: wrap; gap: 8px 18px; color: var(--text-dim); margin-top: 10px; }
.detail-meta strong { color: var(--text); }

.sec-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px; flex-wrap: wrap; margin: 26px 0 10px;
}
.sec-head h2, .detail section > h2 { margin: 0; font-size: 1.15rem; }

.ingredients { list-style: none; margin: 0; padding: 0; }
.ingredients li {
  display: flex; align-items: baseline; gap: 8px;
  padding: 10px 4px; border-bottom: 1px dashed var(--border);
}
.ingredients li:last-child { border-bottom: 0; }
.ingredients .amt { min-width: 96px; color: var(--accent); font-weight: 700; font-variant-numeric: tabular-nums; }
.ingredients .opt { opacity: .6; }
.ingredients .opt-tag { color: var(--text-dim); font-size: .72rem; text-transform: uppercase; letter-spacing: .05em; }

.steps { margin: 0; padding-left: 20px; }
.steps li { margin: 8px 0; padding-left: 6px; }

.detail-actions {
  display: flex; gap: 10px; margin-top: 26px; padding-top: 18px;
  border-top: 1px solid var(--border); align-items: flex-end;
}

/* ---------- Forms ---------- */
.form-card { padding: 22px; }
.form-card h1 { margin: 0 0 16px; font-size: 1.4rem; }
.field { margin-bottom: 16px; }
.field label { display: block; margin-bottom: 6px; color: var(--text-dim); font-size: .85rem; font-weight: 600; }
.form-card input[type=text], .form-card input:not([type]), .form-card input[type=number],
.form-card select, .form-card textarea, #category {
  width: 100%; padding: 11px 13px;
  background: var(--bg-soft); color: var(--text);
  border: 1px solid var(--border); border-radius: 12px;
  font-family: inherit; font-size: .95rem; outline: none;
}
.form-card input:focus, .form-card select:focus, .form-card textarea:focus {
  border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft);
}
textarea { resize: vertical; min-height: 140px; }

#ing-editor .ing-row {
  display: grid; grid-template-columns: 1fr 76px 90px 34px; gap: 8px;
  margin-bottom: 8px; align-items: center;
}
#ing-editor .ing-row input { padding: 9px 10px; }
.ing-row .opt-label { height: 34px; display: grid; place-items: center; cursor: pointer; font-size: .8rem; }
.ing-row input[type=checkbox] { accent-color: var(--accent); width: 17px; height: 17px; }
.ing-row .remove-btn {
  background: none; border: 0; color: var(--text-dim); cursor: pointer;
  font-size: 1.1rem; padding: 6px; line-height: 1;
}
.ing-row .remove-btn:hover { color: var(--danger); }

.form-actions { display: flex; gap: 10px; margin-top: 20px; }
.alert {
  padding: 10px 14px; border-radius: 12px; margin-bottom: 14px;
  background: rgba(226, 92, 92, .12); color: var(--danger);
  border: 1px solid rgba(226, 92, 92, .3);
}

/* ---------- Recipe photos ---------- */
.hide { display: none !important; }
.rcard-img { margin: -4px 0 14px; border-radius: 12px; overflow: hidden; }
.rcard-img img { display: block; width: 100%; height: 150px; object-fit: cover; }
.detail-img img {
  display: block; width: 100%; max-height: 460px; object-fit: cover;
  border-radius: 14px; margin: 6px 0 4px;
}
.img-field { display: flex; flex-direction: column; gap: 10px; align-items: flex-start; }
.img-preview { max-width: 300px; max-height: 220px; border-radius: 12px; object-fit: cover; }
.img-pick {
  position: relative; cursor: pointer;
  display: inline-block; padding: 13px 18px;
  background: var(--accent-soft); color: var(--accent);
  border: 1px dashed var(--accent); border-radius: 12px;
  font-weight: 700; font-size: .95rem;
}
.img-pick input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.img-remove { color: var(--text-dim); font-size: .88rem; display: inline-flex; gap: 6px; align-items: center; }
.img-remove.dim { opacity: .5; pointer-events: none; }
.img-remove input { accent-color: var(--danger); }

/* ---------- Shopping list ---------- */
.addform { padding: 14px; margin-top: 4px; box-shadow: none; }
#add-item-form { display: flex; gap: 8px; flex-wrap: wrap; }
#add-item-form input {
  flex: 1 1 120px; min-width: 0; padding: 11px 13px;
  background: var(--bg-soft); color: var(--text);
  border: 1px solid var(--border); border-radius: 12px;
  font-family: inherit; font-size: .95rem; outline: none;
}
#add-item-form input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
#add-item-form #new-name { flex-basis: 220px; }

.shop-list { list-style: none; margin: 0; padding: 4px 8px; }
.shop-list li {
  display: flex; align-items: center; gap: 12px;
  padding: 13px 10px; border-bottom: 1px dashed var(--border);
}
.shop-list li:last-child { border-bottom: 0; }
.shop-list input[type=checkbox] {
  width: 22px; height: 22px; flex: none;
  accent-color: var(--accent); cursor: pointer;
}
.shop-list .nm { font-weight: 600; flex: 1; min-width: 0; word-break: break-word; }
.shop-list .qty { color: var(--accent); font-weight: 700; font-variant-numeric: tabular-nums; }
.shop-list .src { display: block; font-size: .78rem; color: var(--text-dim); font-weight: 400; }
.shop-list .del {
  background: none; border: 0; cursor: pointer;
  color: var(--text-dim); font-size: 1.05rem; padding: 6px;
}
.shop-list .del:hover { color: var(--danger); }
.shop-list li.checked .nm, .shop-list li.checked .qty { text-decoration: line-through; opacity: .45; }
.shop-list li.checked { background: transparent; }

/* View switcher + grouping */
.viewbar { margin: 12px 0 4px; }
.seg {
  display: inline-flex; gap: 4px; padding: 4px;
  background: var(--card); border: 1px solid var(--border); border-radius: 14px;
}
.seg-btn {
  border: 0; background: transparent; color: var(--text-dim);
  font-family: inherit; font-size: .88rem; font-weight: 700;
  padding: 8px 14px; border-radius: 10px; cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.seg-btn.on { background: var(--accent-soft); color: var(--accent); }
.hint { color: var(--text-dim); font-size: .82rem; margin: 8px 0 0; }

.group-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 10px 6px; font-weight: 800; color: var(--accent);
  font-size: .95rem; position: sticky; top: 52px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px); z-index: 2;
}
.group-head small { color: var(--text-dim); font-weight: 600; }

.shop-list li.mrow {
  position: relative; cursor: pointer;
  display: grid; grid-template-columns: auto auto 1fr auto auto;
  align-items: center; gap: 12px;
}
.shop-list .merge-tag {
  display: inline-block; margin-left: 8px; padding: 1px 8px;
  border-radius: 999px; background: var(--accent-soft); color: var(--accent);
  font-size: .72rem; font-weight: 700; vertical-align: 2px;
}
.shop-list .expand {
  background: none; border: 0; color: var(--text-dim); cursor: pointer;
  font-size: .85rem; padding: 4px 6px; line-height: 1; transition: transform .15s ease;
}
.shop-list li.mrow.open .expand { transform: rotate(180deg); color: var(--accent); }
.breakdown {
  display: none; grid-row: 2; grid-column: 1 / -1;
  border-top: 1px dashed var(--border); margin-top: 6px; padding: 6px 4px 2px;
}
.shop-list li.mrow.open .breakdown { display: block; }
.breakdown .brow {
  color: var(--text-dim); font-size: .83rem; padding: 3px 0;
}
.breakdown .brow-done { text-decoration: line-through; opacity: .5; }

.empty { text-align: center; padding: 40px 20px; }
.empty .big { font-size: 3rem; margin-bottom: 8px; }
.loader { color: var(--text-dim); text-align: center; padding: 30px 0; }

.btn-group { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---------- Bottom nav (mobile) ---------- */
@media (max-width: 640px) {
  .nav a { font-size: .85rem; padding: 6px 10px; }
}

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(120%);
  background: var(--card-2); color: var(--text);
  padding: 11px 18px; border-radius: 999px;
  border: 1px solid var(--border); box-shadow: var(--shadow);
  font-weight: 600; font-size: .92rem; z-index: 100;
  transition: transform .25s ease; white-space: nowrap; max-width: 90vw; overflow: hidden; text-overflow: ellipsis;
}
.toast.show { transform: translateX(-50%) translateY(0); }

.hide-on-desktop { display: none; }
@media (max-width: 640px) { .js-back { display: inline; } .hide-on-desktop { display: inline; } }

/* ---------- Size tuning ----------
   Base is tuned for phones (14px). Tablets/laptops scale up via rem;
   the pixel-based overrides below do the rest. */

/* Phones — slightly more compact */
@media (max-width: 480px) {
  .topbar { padding: 8px 12px; gap: 10px; }
  .brand { font-size: .95rem; }
  .brand-mark { width: 28px; height: 28px; }
  .nav a { padding: 6px 9px; font-size: .85rem; }
  .add-btn { padding: 8px 12px; font-size: .88rem; }
  .wrap { padding: 14px 12px 72px; }
  .hero h1 { font-size: 1.45rem; margin-bottom: 10px; }
  .search-icon { width: 18px; height: 18px; left: 12px; }
  #search, .searchbar input { padding: 11px 13px 11px 38px; font-size: .95rem; border-radius: 12px; }
  .chips { gap: 7px; }
  .chip { padding: 6px 11px; font-size: .82rem; }
  .rcard { padding: 14px 15px; }
  .rcard h2 { font-size: 1rem; }
  .rcard .meta { font-size: .8rem; }
  .rcard .badge { width: 36px; height: 36px; font-size: 1rem; }
  .btn { padding: 9px 13px; font-size: .9rem; }
  .btn-sm { padding: 5px 10px; font-size: .8rem; }
  .detail { padding: 18px 15px; }
  .detail-head h1 { font-size: 1.5rem; }
  .ingredients .amt { min-width: 80px; font-size: .92rem; }
  .card { border-radius: 14px; }
  #add-item-form input { font-size: .9rem; padding: 10px 11px; }
  .seg-btn { padding: 7px 11px; font-size: .83rem; }
  .shop-list li { padding: 12px 8px; }
  .group-head { top: 46px; }
  .form-card { padding: 17px 15px; }
}

/* Desktop — roomier */
@media (min-width: 1000px) {
  .topbar { padding: 15px 26px; gap: 20px; }
  .brand { font-size: 1.25rem; gap: 10px; }
  .brand-mark { width: 40px; height: 40px; border-radius: 12px; font-size: 1.35rem; }
  .nav { gap: 10px; }
  .nav a { padding: 10px 18px; font-size: 1.05rem; }
  .add-btn { padding: 13px 22px; font-size: 1.05rem; border-radius: 14px; }
  .wrap { max-width: 1120px; padding: 30px 30px 120px; }
  .hero h1 { font-size: 2.5rem; margin-bottom: 18px; }
  .sub { font-size: 1.1rem; }
  .searchbar { max-width: 680px; }
  .search-icon { width: 26px; height: 26px; left: 18px; }
  #search, .searchbar input { padding: 17px 22px 17px 56px; font-size: 1.15rem; border-radius: 16px; }
  .chips { gap: 11px; margin: 22px 0 8px; }
  .chip { padding: 10px 18px; font-size: .98rem; border-radius: 999px; }
  .list-toolbar { margin: 24px 0 14px; font-size: 1.05rem; }
  #sort { padding: 11px 14px; font-size: 1.02rem; }
  .count { font-size: 1.02rem; }
  .cards { gap: 20px; grid-template-columns: repeat(auto-fill, minmax(330px, 1fr)); }
  .rcard { padding: 24px 26px; border-radius: 20px; }
  .rcard h2 { font-size: 1.35rem; margin: 10px 0 6px; }
  .rcard .meta { font-size: 1rem; }
  .rcard .badge { width: 48px; height: 48px; border-radius: 14px; font-size: 1.3rem; }
  .btn { padding: 13px 22px; font-size: 1.05rem; border-radius: 14px; }
  .btn-sm { padding: 8px 14px; font-size: .9rem; }
  .detail { padding: 34px; border-radius: 22px; }
  .detail-head h1 { font-size: 2.6rem; }
  .detail-meta { font-size: 1.05rem; gap: 12px 26px; }
  .sec-head h2 { font-size: 1.45rem; }
  .ingredients li { padding: 13px 6px; font-size: 1.1rem; }
  .ingredients .amt { min-width: 130px; }
  .steps li { font-size: 1.05rem; margin: 10px 0; }
  .form-card { padding: 32px; border-radius: 22px; }
  .form-card h1 { font-size: 1.8rem; }
  #ing-editor .ing-row input { padding: 12px 12px; }
  #add-item-form input { font-size: 1.05rem; padding: 13px 14px; }
  .shop-list li { padding: 15px 12px; font-size: 1.05rem; }
  .shop-list .qty { font-size: 1.05rem; }
  .shop-list input[type=checkbox] { width: 26px; height: 26px; }
  .group-head { font-size: 1.1rem; padding: 18px 12px 8px; top: 62px; }
  .breakdown .brow { font-size: .95rem; }
  .seg-btn { padding: 12px 20px; font-size: 1rem; }
  .hint { font-size: .95rem; }
  .toast { font-size: 1.05rem; padding: 14px 24px; }
}
/* URL import bar (Add recipe page) */
.import-bar {
  margin: 14px 0 4px;
  padding: 12px 14px;
  border: 1px dashed var(--accent, #e9643a);
  border-radius: 12px;
  background: rgba(233, 100, 58, .07);
}
.import-row { display: flex; gap: 10px; align-items: center; }
.import-row input[type=url] {
  flex: 1;
  min-width: 0;
  padding: 10px 12px;
  font-size: 1rem;
  border: 1px solid var(--border, rgba(255,255,255,.14));
  border-radius: 10px;
  background: var(--card-bg, rgba(30,28,24,.9));
  color: var(--text, #f3ead8);
}
.import-row input[type=url]:focus { outline: none; border-color: var(--accent, #e9643a); }
.import-status { min-height: 1.2em; margin: 8px 0 0; font-size: .9rem; color: var(--muted, #9a9386); }
.import-status.ok { color: #8fd18f; }
.import-status.err { color: #ff8f7a; }

/* Login screen */
.login-body { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.login-card {
  width: min(360px, 100%);
  padding: 30px 26px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  text-align: center;
  border-radius: 18px;
  box-sizing: border-box;
}
.login-mark { font-size: 2.6rem; line-height: 1; }
.login-card h1 { margin: 0; font-size: 1.4rem; }
.login-sub { margin: -6px 0 4px; color: var(--text-dim); font-size: .95rem; }
.login-input {
  width: 100%;
  box-sizing: border-box;
  padding: 12px 14px;
  font-size: 1.05rem;
  color: var(--text);
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: 10px;
}
.login-input:focus { outline: none; border-color: var(--accent); }
.login-btn { width: 100%; }
.logout-btn { font-size: .85rem; padding: 8px 12px; opacity: .8; }
