/* =========================================================================
   ZeroRecipe — light, warm, food-friendly theme. No frameworks.
   ========================================================================= */
:root {
  --bg: #faf7f2;
  --surface: #ffffff;
  --ink: #2d2a26;
  --ink-soft: #6b6459;
  --line: #e8e2d8;
  --accent: #c2571b;
  --accent-strong: #a34413;
  --accent-soft: #fbe9dd;
  --good: #2e7d4f;
  --danger: #b3403a;
  --danger-soft: #f7e1e0;
  --star: #e8a33d;
  --header-bg: rgba(255, 255, 255, 0.92);
  --card-hover-line: #dcd4c6;
  --btn-danger-bg: #ffffff;
  --input-bg: #ffffff;
  --toast-bg: #2d2a26;
  --toast-ink: #ffffff;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(45, 42, 38, 0.06), 0 4px 16px rgba(45, 42, 38, 0.06);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family:
    ui-sans-serif,
    system-ui,
    -apple-system,
    "Segoe UI",
    Roboto,
    "Helvetica Neue",
    sans-serif;
  background: var(--bg);
  color: var(--ink);
  line-height: 1.55;
}

h1,
h2,
h3 {
  font-family: Georgia, "Times New Roman", serif;
  letter-spacing: -0.01em;
  line-height: 1.25;
}

h1 {
  font-size: 1.75rem;
  margin: 0.2rem 0 0.1rem;
}
h2 {
  font-size: 1.3rem;
  margin: 1.4rem 0 0.6rem;
}
h3 {
  font-size: 1.1rem;
}

.container {
  max-width: 1040px;
  margin: 0 auto;
  padding: 0 1.1rem;
}

#view {
  min-height: 55vh;
  padding-top: 1.4rem;
  padding-bottom: 3rem;
}

/* ---------- header / nav ---------- */
.site-header {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--header-bg);
  backdrop-filter: blur(6px);
  border-bottom: 1px solid var(--line);
}

.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  height: 56px;
}

.brand {
  font-family: Georgia, serif;
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--ink);
  text-decoration: none;
  white-space: nowrap;
}

.main-nav {
  display: flex;
  align-items: center;
  gap: 0.35rem;
}

.main-nav a {
  color: var(--ink-soft);
  text-decoration: none;
  padding: 0.4rem 0.7rem;
  border-radius: 8px;
  font-size: 0.95rem;
}

.main-nav a:hover {
  background: var(--accent-soft);
  color: var(--ink);
}

.main-nav a.active {
  color: var(--accent-strong);
  font-weight: 600;
  background: var(--accent-soft);
}

.theme-toggle {
  display: inline-grid;
  place-items: center;
  width: 34px;
  height: 34px;
  padding: 0;
  border-radius: 50%;
  font-size: 1rem;
  line-height: 1;
}

/* ---------- buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  border-radius: 9px;
  padding: 0.5rem 0.95rem;
  font-size: 0.95rem;
  font-family: inherit;
  cursor: pointer;
  text-decoration: none;
  transition:
    background 0.12s,
    border-color 0.12s,
    transform 0.05s;
}

.btn:hover {
  background: var(--bg);
}

.btn:active {
  transform: translateY(1px);
}

.btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.btn-primary:hover {
  background: var(--accent-strong);
}

.btn-danger {
  color: var(--danger);
  border-color: var(--danger-soft);
  background: var(--btn-danger-bg);
}

.btn-danger:hover {
  background: var(--danger-soft);
}

.btn-sm {
  padding: 0.35rem 0.7rem;
  font-size: 0.88rem;
  border-radius: 8px;
}

/* ---------- cards / grid ---------- */
.page-title {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}

.page-sub {
  color: var(--ink-soft);
  margin: 0.2rem 0 1.2rem;
}

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(270px, 1fr));
  gap: 1rem;
}

.card {
  display: flex;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1rem 1.1rem;
  flex-direction: column;
  gap: 0.55rem;
  text-align: left;
  cursor: pointer;
  font: inherit;
  color: inherit;
  text-decoration: none;
  transition:
    transform 0.12s,
    border-color 0.12s;
}

.card:hover {
  transform: translateY(-2px);
  border-color: var(--card-hover-line);
}

.card h3 {
  margin: 0;
}

.card p {
  margin: 0;
  color: var(--ink-soft);
  font-size: 0.92rem;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.card-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.45rem;
  font-size: 0.85rem;
  color: var(--ink-soft);
  margin-top: auto;
}

/* ---------- chips / stars ---------- */
.chip {
  display: inline-block;
  background: var(--accent-soft);
  color: var(--accent-strong);
  border-radius: 999px;
  padding: 0.1rem 0.65rem;
  font-size: 0.8rem;
  font-weight: 600;
}

.stars {
  color: var(--star);
  letter-spacing: 0.08em;
  font-size: 0.95rem;
}

.rating-line {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
}

.rating-line .count {
  color: var(--ink-soft);
  font-size: 0.85rem;
}

/* ---------- interactive rating ---------- */
.rate-box {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  flex-wrap: wrap;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0.65rem 0.9rem;
}

.rate-box-label {
  font-weight: 650;
  font-size: 0.9rem;
}

.rate-stars {
  display: inline-flex;
  gap: 0.05rem;
}

.rate-star {
  appearance: none;
  background: none;
  border: 0;
  padding: 0.1rem 0.25rem;
  font-size: 1.55rem;
  line-height: 1;
  color: var(--star);
  opacity: 0.25;
  cursor: pointer;
  transition: opacity 0.12s ease, transform 0.12s ease;
}

.rate-star:hover,
.rate-star:focus-visible {
  opacity: 0.75;
  transform: scale(1.12);
  outline: none;
}

.rate-star.active {
  opacity: 1;
}

.rate-star:disabled {
  cursor: wait;
  opacity: 0.4;
}

/* ---------- forms ---------- */
.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.3rem 1.4rem;
  max-width: 720px;
}

.field {
  margin-bottom: 1.05rem;
}

.field label {
  display: block;
  font-weight: 600;
  font-size: 0.92rem;
  margin-bottom: 0.3rem;
}

.field .hint {
  font-weight: 400;
  color: var(--ink-soft);
  font-size: 0.83rem;
}

input[type="text"],
input[type="password"],
input[type="number"],
input[type="email"],
input[type="date"],
textarea,
select {
  width: 100%;
  font: inherit;
  color: inherit;
  background: var(--input-bg);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 0.55rem 0.7rem;
}

input:focus,
textarea:focus,
select:focus {
  outline: 2px solid var(--accent-soft);
  border-color: var(--accent);
}

textarea {
  min-height: 92px;
  resize: vertical;
}

.form-row {
  display: flex;
  gap: 0.9rem;
  flex-wrap: wrap;
}

.form-row .field {
  flex: 1 1 140px;
}

.checklist {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem 1rem;
}

.checklist label {
  display: inline-flex;
  gap: 0.4rem;
  align-items: center;
  font-weight: 400;
  font-size: 0.92rem;
}

/* ---------- prerequisites on the detail page ---------- */
.prereq-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}

.chip-link {
  text-decoration: none;
  cursor: pointer;
}

.chip-link:hover {
  filter: brightness(0.95);
}

/* ---------- prerequisites in the add/edit form ---------- */
.prereq-added {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  margin-bottom: 0.4rem;
}

.prereq-added:empty {
  display: none;
}

.prereq-row {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  background: var(--accent-soft);
  border-radius: 8px;
  padding: 0.35rem 0.5rem 0.35rem 0.7rem;
}

.prereq-row-title {
  flex: 1;
  font-weight: 600;
  font-size: 0.92rem;
}

.prereq-added .muted {
  margin: 0;
}

.prereq-add-row {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

.prereq-add-row select {
  flex: 1;
  min-width: 0;
}

.form-actions {
  display: flex;
  gap: 0.6rem;
  margin-top: 0.4rem;
}

/* ---------- search bar ---------- */
.search-bar {
  display: flex;
  gap: 0.6rem;
  flex-wrap: wrap;
  margin-bottom: 1.4rem;
}

.search-bar input[type="text"] {
  flex: 2 1 260px;
}

.search-bar select {
  flex: 1 1 160px;
  width: auto;
}

/* ---------- detail ---------- */
.detail {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.6rem 1.8rem;
}

.detail-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 1.4rem;
  color: var(--ink-soft);
  font-size: 0.95rem;
  margin: 0.7rem 0 1.1rem;
}

.detail-actions {
  display: flex;
  gap: 0.6rem;
  margin-top: 1.4rem;
  padding-top: 1.1rem;
  border-top: 1px solid var(--line);
}

.ing-wrap .ing-list {
  padding-left: 1.2rem;
  columns: 2;
  column-gap: 2.4rem;
  margin: 0 0 0.35rem;
}

.ing-wrap .ing-list li {
  break-inside: avoid;
}

.ing-wrap .ing-heading {
  margin: 0.65rem 0 0.3rem;
  font-weight: 600;
  font-size: 0.95rem;
}

.ing-wrap .ing-heading:first-child {
  margin-top: 0;
}

.instructions .ins-text {
  white-space: pre-wrap;
}

.instructions .ins-heading {
  margin: 0.65rem 0 0.3rem;
  font-weight: 600;
  font-size: 0.95rem;
}

.instructions .ins-heading:first-child {
  margin-top: 0;
}

/* ---------- categories page ---------- */
.cat-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.cat-row {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0.7rem 1rem;
}

.cat-row .name {
  font-weight: 600;
  flex: 1;
}

.cat-row a.name {
  color: inherit;
  text-decoration: none;
  cursor: pointer;
}

.cat-row a.name:hover {
  color: var(--accent);
  text-decoration: underline;
}

.cat-row .cat-controls {
  display: flex;
  gap: 0.4rem;
}

/* ---------- shopping list ---------- */
.shopping-panel {
  max-width: 560px;
}

.shopping-add-row {
  display: flex;
  gap: 0.5rem;
}

.shopping-add-row input {
  flex: 1;
  min-width: 0;
}

.shopping-empty {
  margin: 1rem 0 0;
}

.shopping-items {
  list-style: none;
  margin: 1rem 0 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.shopping-item {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 0.45rem 0.5rem 0.45rem 0.7rem;
}

.shopping-item label {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  flex: 1;
  min-width: 0;
  cursor: pointer;
}

.shopping-item input[type="checkbox"] {
  width: 1.05rem;
  height: 1.05rem;
  flex: none;
  cursor: pointer;
  accent-color: var(--accent);
}

.shopping-item-text {
  overflow-wrap: anywhere;
}

.shopping-item.done {
  background: var(--accent-soft);
  border-color: var(--accent-soft);
}

.shopping-item.done .shopping-item-text {
  text-decoration: line-through;
  color: var(--ink-soft);
}

.shopping-item-del {
  flex: none;
}

.shopping-progress {
  margin: 0.8rem 0 0;
  font-size: 0.88rem;
}

.shopping-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem;
  flex-wrap: wrap;
  margin-top: 1rem;
  padding-top: 0.9rem;
  border-top: 1px solid var(--line);
}

/* ---- shopping history ---- */
.shopping-history {
  margin-top: 2.2rem;
}

.history-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.history-entry {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.history-head {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  width: 100%;
  background: none;
  border: 0;
  font: inherit;
  color: inherit;
  text-align: left;
  padding: 0.75rem 1rem;
  cursor: pointer;
}

.history-head:hover {
  background: var(--bg);
}

.history-head-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.history-head-title {
  font-weight: 600;
}

.history-head-sub {
  font-size: 0.85rem;
  color: var(--ink-soft);
}

.history-caret {
  color: var(--ink-soft);
  transform: rotate(-90deg);
  transition: transform 0.15s ease;
}

.history-entry.open .history-caret {
  transform: none;
}

.history-items {
  list-style: none;
  margin: 0;
  padding: 0.6rem 1rem 0.8rem;
  border-top: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

/* The `display: flex` above would override the HTML `hidden` attribute's
   default `display: none`; this rule re-asserts it so collapsed lists stay
   hidden. */
.history-items[hidden] {
  display: none;
}

.history-item {
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
  font-size: 0.93rem;
  padding: 0.15rem 0;
}

.history-item .tick {
  width: 1.1rem;
  flex: none;
  font-weight: 600;
  color: var(--good);
}

.history-item .tick.miss {
  color: var(--ink-soft);
}

.history-item .hist-text.miss {
  color: var(--ink-soft);
}

/* ---------- add-ingredients dialog (recipe page) ---------- */
.ingredient-dialog {
  width: min(480px, calc(100vw - 2rem));
  max-height: min(70vh, 560px);
  padding: 1.1rem 1.2rem;
  background: var(--surface);
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

/* An author `display` would override the UA's closed-dialog `display: none`,
   so apply flex only while the dialog is actually open. */
.ingredient-dialog[open] {
  display: flex;
  flex-direction: column;
  gap: 0.8rem;
}

.ingredient-dialog::backdrop {
  background: rgba(45, 42, 38, 0.45);
}

.ingredient-dialog h2 {
  margin: 0;
  font-size: 1.2rem;
}

.pick-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.8rem;
}

.pick-list {
  overflow-y: auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.pick-item {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 0.45rem 0.6rem;
  cursor: pointer;
}

.pick-item:hover {
  border-color: var(--card-hover-line);
}

.pick-item input[type="checkbox"] {
  width: 1.05rem;
  height: 1.05rem;
  flex: none;
  accent-color: var(--accent);
  cursor: pointer;
}

.pick-text {
  overflow-wrap: anywhere;
}

.pick-foot {
  display: flex;
  justify-content: flex-end;
  gap: 0.6rem;
}

/* ---------- users (admin) ---------- */
.user-list {
  list-style: none;
  margin: 0.4rem 0 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.user-row {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.45rem 0.1rem;
  border-bottom: 1px solid var(--line);
}

.user-row:last-child {
  border-bottom: 0;
}

.user-name {
  font-weight: 600;
  flex: 1;
  min-width: 0;
  overflow-wrap: anywhere;
}

.user-note {
  font-size: 0.85rem;
}

.user-create-row {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  align-items: center;
  margin: 0.6rem 0 0.4rem;
}

.user-create-row input {
  flex: 1;
  min-width: 140px;
}

/* ---------- states ---------- */
.empty,
.loading,
.error-box {
  padding: 2.2rem 1rem;
  text-align: center;
  color: var(--ink-soft);
  background: var(--surface);
  border: 1px dashed var(--line);
  border-radius: var(--radius);
}

.error-box {
  border-color: var(--danger-soft);
  color: var(--danger);
  background: var(--danger-soft);
}

.error-box ul {
  margin: 0.6rem 0 0;
  padding-left: 1.2rem;
  text-align: left;
}

.muted {
  color: var(--ink-soft);
}

/* ---------- toasts ---------- */
.toasts {
  position: fixed;
  bottom: 1.2rem;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  z-index: 50;
}

.toast {
  background: var(--toast-bg);
  color: var(--toast-ink);
  padding: 0.6rem 1.1rem;
  border-radius: 10px;
  box-shadow: var(--shadow);
  font-size: 0.92rem;
  animation: toast-in 0.18s ease-out;
}

.toast.success {
  background: var(--good);
}

.toast.error {
  background: var(--danger);
}

@keyframes toast-in {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

/* ---------- footer ---------- */
.site-footer {
  border-top: 1px solid var(--line);
  padding: 1rem 0 1.4rem;
  color: var(--ink-soft);
  font-size: 0.88rem;
}

/* ---------- backup page ---------- */
.backup-panel {
  max-width: 720px;
}

.backup-panel + .backup-panel {
  margin-top: 1rem;
}

.danger-note {
  color: var(--danger);
  background: var(--danger-soft);
  border: 1px solid var(--danger-soft);
  border-radius: 9px;
  padding: 0.6rem 0.85rem;
  font-size: 0.92rem;
}

.backup-errors {
  margin-top: 1rem;
}

/* =========================================================================
   Dark theme — activated with data-theme="dark" on <html>.
   The boot script in index.html applies the saved theme before first paint
   (falling back to prefers-color-scheme); app.js handles the toggle and
   persistence (localStorage key: zerorecipe-theme). Every component reads
   from the custom properties below, so this whole block re-skins the UI.
   ========================================================================= */
html[data-theme="dark"] {
  color-scheme: dark;
  --bg: #16130f;
  --surface: #201b15;
  --ink: #f2ece2;
  --ink-soft: #a99f90;
  --line: #3a332a;
  --accent: #e0662c;
  --accent-strong: #ef8a4e;
  --accent-soft: rgba(224, 102, 44, 0.16);
  --good: #5cb87a;
  --danger: #e06a5f;
  --danger-soft: rgba(224, 106, 95, 0.18);
  --star: #f0b04e;
  --header-bg: rgba(28, 23, 18, 0.92);
  --card-hover-line: #4d4436;
  --btn-danger-bg: #201b15;
  --input-bg: #29231c;
  --toast-bg: #f2ece2;
  --toast-ink: #241f18;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.5), 0 4px 16px rgba(0, 0, 0, 0.35);
}

/* ---------- auth ---------- */
.auth-area {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-left: 0.5rem;
  padding-left: 0.9rem;
  border-left: 1px solid var(--line);
}

.auth-user {
  font-size: 0.85rem;
  white-space: nowrap;
}

.auth-panel {
  max-width: 440px;
}

.gate-box {
  max-width: 520px;
}

.gate-box .form-actions {
  margin-top: 1rem;
}

/* =========================================================================
   Mobile — kept last so these overrides win over every base rule above.
   ========================================================================= */
@media (max-width: 640px) {
  .header-inner {
    height: auto;
    flex-direction: column;
    padding: 0.55rem 0;
    gap: 0.3rem;
  }

  /* The nav is wider than a phone screen, so let it scroll horizontally.
     It must be width-capped and start-aligned: with the header's default
     `align-items: center`, overflow spills off both edges, and left-side
     overflow (Home, Search) can't be scrolled back into view. */
  .main-nav {
    width: 100%;
    max-width: 100%;
    flex-wrap: nowrap;
    justify-content: flex-start;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
    padding-bottom: 0.15rem; /* keep the scrollbar from covering labels */
  }

  /* Items keep their natural width and the strip scrolls horizontally.
     Without `flex: none`, flex shrinks each item to min-content before
     overflowing, wrapping labels like “+ Add recipe” onto two lines. */
  .main-nav > * {
    flex: none;
    white-space: nowrap;
  }

  .main-nav::-webkit-scrollbar {
    height: 4px;
  }

  /* Username + actions sit on their own row here; wrap instead of
     overflowing and drop the divider designed for the desktop row. */
  .auth-area {
    flex-wrap: wrap;
    justify-content: center;
    max-width: 100%;
    margin-left: 0;
    padding-left: 0;
    border-left: 0;
  }

  .detail {
    padding: 1.2rem 1.1rem;
  }

  .ing-list {
    columns: 1;
  }
}
