:root {
  --bg: #fffaf3;
  --surface: #ffffff;
  --surface-2: #f8efe3;
  --fg: #2b2118;
  --muted: #7a6a5a;
  --line: #eadfce;
  --accent: #c2562b;
  --accent-ink: #ffffff;
  --ok: #2f7a4a;
  --ok-bg: #e6f4ea;
  --danger: #b42318;
  --danger-bg: #fdecea;
  --radius: 12px;
  --shadow: 0 1px 3px rgb(0 0 0 / 0.06), 0 6px 16px rgb(0 0 0 / 0.04);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1612;
    --surface: #261e18;
    --surface-2: #30261e;
    --fg: #f4eadf;
    --muted: #b3a493;
    --line: #3d3128;
    --accent: #e0774a;
    --ok: #7fd09a;
    --ok-bg: #1f3326;
    --danger: #ff8a80;
    --danger-bg: #3a1f1c;
  }
}

*, *::before, *::after { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 16px/1.55 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); }
h1, h2, h3 { line-height: 1.2; }
h1 { font-size: 1.7rem; margin: 0 0 1rem; }
h2 { font-size: 1.25rem; margin: 2rem 0 0.75rem; }

/* Layout */
.topbar {
  display: flex; align-items: center; gap: 1rem; flex-wrap: wrap;
  padding: 0.75rem 1.25rem;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 10;
}
.brand { font-weight: 800; color: var(--accent); text-decoration: none; font-size: 1.1rem; }
.nav { display: flex; gap: 0.25rem; flex-wrap: wrap; flex: 1; }
.nav a {
  color: var(--fg); text-decoration: none; padding: 0.35rem 0.7rem; border-radius: 999px;
}
.nav a:hover { background: var(--surface-2); }
.nav a[aria-current='page'] { background: var(--accent); color: var(--accent-ink); }
.nav .soon { color: var(--muted); padding: 0.35rem 0.7rem; cursor: default; }
.userbox { display: flex; align-items: center; gap: 0.75rem; color: var(--muted); font-size: 0.9rem; }

.container { max-width: 960px; margin: 0 auto; padding: 1.75rem 1.25rem 4rem; }
.narrow { max-width: 420px; }

/* Components */
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 1.25rem;
}
.grid { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }
.stack > * + * { margin-top: 0.9rem; }
.row { display: flex; gap: 0.5rem; align-items: center; flex-wrap: wrap; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }

.badge {
  display: inline-block; font-size: 0.75rem; font-weight: 600; padding: 0.1rem 0.5rem;
  border-radius: 999px; background: var(--surface-2); color: var(--muted);
}
.badge.admin { background: var(--accent); color: var(--accent-ink); }
.badge.danger { background: var(--danger-bg); color: var(--danger); }

.alert { padding: 0.75rem 1rem; border-radius: 10px; margin-bottom: 1rem; }
.alert.ok { background: var(--ok-bg); color: var(--ok); }
.alert.error { background: var(--danger-bg); color: var(--danger); }

/* Forms */
label { display: block; font-weight: 600; font-size: 0.9rem; margin-bottom: 0.3rem; }
input, select, textarea {
  width: 100%; font: inherit; color: inherit; padding: 0.55rem 0.7rem;
  border: 1px solid var(--line); border-radius: 8px; background: var(--bg);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
input[aria-invalid='true'] { border-color: var(--danger); }
.field-error { color: var(--danger); font-size: 0.85rem; margin: 0.25rem 0 0; }

button, .button {
  display: inline-flex; align-items: center; justify-content: center; gap: 0.35rem;
  font: inherit; font-weight: 600; cursor: pointer; text-decoration: none;
  padding: 0.55rem 1rem; border-radius: 8px; border: 1px solid transparent;
  background: var(--accent); color: var(--accent-ink);
}
button:hover, .button:hover { filter: brightness(1.05); }
button.secondary, .button.secondary { background: var(--surface-2); color: var(--fg); border-color: var(--line); }
button.danger { background: var(--danger-bg); color: var(--danger); border-color: transparent; }
button.link { background: none; color: var(--accent); padding: 0; border: 0; font-weight: 500; }
button.small, .button.small { padding: 0.3rem 0.6rem; font-size: 0.85rem; }
form.inline { display: inline; }

/* Tables */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 0.65rem 0.6rem; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }

details > summary { cursor: pointer; color: var(--accent); font-size: 0.9rem; }
details[open] > summary { margin-bottom: 0.5rem; }

.field-hint { color: var(--muted); font-size: 0.8rem; margin: 0.25rem 0 0; }
textarea { resize: vertical; }
code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.85em; background: var(--surface-2); padding: 0.1rem 0.35rem; border-radius: 6px; }

.page-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-bottom: 1rem; }
.page-head h1 { margin: 0; }
.grow { flex: 1; min-width: 12rem; }
.nowrap { flex-wrap: nowrap; }

/* Filtros / búsqueda */
.filters { display: flex; gap: 0.75rem; align-items: flex-end; flex-wrap: wrap; margin-bottom: 1rem; }
.filters select { width: auto; min-width: 11rem; }

/* Formularios en 2 columnas */
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.9rem 1rem; }
.form-grid.stack > * + * { margin-top: 0; }
.form-grid .span-2 { grid-column: 1 / -1; }

.pager { justify-content: center; margin-top: 1rem; }

/* Catálogos */
.subnav { margin-bottom: 1rem; }
.subnav a { padding: 0.25rem 0.7rem; border-radius: 999px; background: var(--surface-2); text-decoration: none; font-size: 0.9rem; }
.catalog-section { scroll-margin-top: 5rem; }
.catalog-section td input, .catalog-section td select { padding: 0.35rem 0.5rem; }
.col-order { width: 6rem; }
.col-num { width: 7rem; }
.add-form { margin-top: 0.75rem; align-items: flex-end; }
.card > p:first-child { margin-top: 0; }
.catalog-section td select { min-width: 6.5rem; }

.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); border: 0; }
.space-between { justify-content: space-between; }
.card-title { margin: 0 0 0.5rem; font-size: 1.1rem; }
.mobile-only { display: none; }
label.check { display: flex; gap: 0.4rem; align-items: center; font-weight: 500; margin: 0; padding-bottom: 0.6rem; }
label.check input { width: auto; }
.empty-state { text-align: center; padding: 2rem; }

/* Editor de recetas */
.recipe-form > .card { margin-top: 1rem; }
.row.three { align-items: flex-end; }
.row.three > * { flex: 1; min-width: 0; }
.ing-table { display: grid; gap: 0.4rem; margin-bottom: 0.75rem; }
.ing-row {
  display: grid; gap: 0.4rem; align-items: center;
  grid-template-columns: 5.5rem 8.5rem minmax(10rem, 2fr) minmax(6rem, 1.2fr) minmax(5rem, 1fr) 2.5rem;
}
.ing-row input, .ing-row select { padding: 0.4rem 0.5rem; }
.ing-head { font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); font-weight: 600; }
.ing-opt { display: flex; justify-content: center; margin: 0; font-weight: 400; }
.ing-opt input { width: auto; }
.ing-error { grid-column: 1 / -1; margin: 0; }
.chips { border: 0; padding: 0; margin: 0; display: flex; flex-wrap: wrap; gap: 0.4rem; }
.chips legend { font-weight: 600; font-size: 0.85rem; margin-bottom: 0.4rem; padding: 0; }
.chip { display: inline-flex; margin: 0; font-weight: 500; font-size: 0.9rem; cursor: pointer; }
.chip input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.chip span { padding: 0.25rem 0.75rem; border-radius: 999px; border: 1px solid var(--line); background: var(--bg); }
.chip input:checked + span { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.chip input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.sticky-actions {
  position: sticky; bottom: 0; z-index: 5; padding: 0.75rem 0; margin-top: 1rem;
  background: linear-gradient(transparent, var(--bg) 30%);
}

/* Fotos */
.photo-grid { display: grid; gap: 0.75rem; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); }
.photo-item { padding: 0; overflow: hidden; margin: 0; }
.photo-item img { display: block; width: 100%; aspect-ratio: 4 / 3; object-fit: cover; }
.photo-item figcaption { padding: 0.5rem; }

/* Listado de recetas */
.recipe-grid { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }
.recipe-card { padding: 0; overflow: hidden; text-decoration: none; color: inherit; display: flex; flex-direction: column; transition: transform 0.1s; }
.recipe-card:hover { transform: translateY(-2px); }
.recipe-card h3 { margin: 0 0 0.25rem; font-size: 1.05rem; }
.recipe-card .muted { margin: 0 0 0.5rem; }
.recipe-card-body { padding: 0.8rem 1rem 1rem; }
.recipe-cover { position: relative; aspect-ratio: 4 / 3; background: var(--surface-2); display: grid; place-items: center; }
.recipe-cover img { width: 100%; height: 100%; object-fit: cover; }
.recipe-cover-empty { font-size: 3rem; opacity: 0.5; }
.fav-badge {
  position: absolute; top: 0.5rem; right: 0.5rem; background: var(--surface); color: var(--accent);
  border-radius: 999px; width: 2rem; height: 2rem; display: grid; place-items: center; box-shadow: var(--shadow);
}
.tag-list { display: flex; flex-wrap: wrap; gap: 0.3rem; }
a.badge { text-decoration: none; }

/* Receta */
.recipe-hero { width: 100%; max-height: 420px; object-fit: cover; border-radius: var(--radius); margin-bottom: 1rem; }
.recipe-header { margin-bottom: 1.5rem; }
.recipe-header .page-head { margin-bottom: 0.25rem; }
.credit { font-style: italic; color: var(--accent); margin: 0.25rem 0; }
button.fav.on { background: var(--accent); }
.recipe-body { display: grid; gap: 1.5rem; grid-template-columns: minmax(260px, 2fr) 3fr; align-items: start; }
.recipe-ingredients { position: sticky; top: 4.5rem; }
.scale-form input { width: 4.5rem; padding: 0.3rem 0.4rem; }
.scale-form label { margin: 0; }
.group-label { font-size: 0.95rem; margin: 1rem 0 0.25rem; color: var(--accent); }
.ingredient-list { list-style: none; padding: 0; margin: 0; }
.ingredient-list li { padding: 0.35rem 0; border-bottom: 1px dashed var(--line); }
.ingredient-list li:last-child { border-bottom: 0; }
.ingredient-list label { display: flex; gap: 0.6rem; align-items: baseline; font-weight: 400; font-size: 1rem; margin: 0; cursor: pointer; }
.ingredient-list input { width: auto; }
.ingredient-list input:checked + span { text-decoration: line-through; opacity: 0.55; }
.steps { padding-left: 1.4rem; }
.steps li { margin-bottom: 0.9rem; padding-left: 0.3rem; font-size: 1.05rem; }
.steps li::marker { font-weight: 700; color: var(--accent); }
.notes { white-space: pre-line; }

/* Menús */
.menu-list { display: grid; gap: 0.6rem; }
.menu-row { display: flex; justify-content: space-between; align-items: center; gap: 1rem; text-decoration: none; color: inherit; padding: 0.9rem 1.1rem; }
.menu-row:hover { border-color: var(--accent); }
.menu-row.current { border-color: var(--accent); }
.menu-row.done { opacity: 0.65; }
.menu-row .badge { margin-left: 0.5rem; }
.right { text-align: right; }
.menu-row progress { width: 6rem; display: block; }
.progress-text { font-weight: 700; font-size: 0.9rem; }
.narrow-wide { max-width: 560px; }

.week-grid { display: grid; gap: 0.4rem; margin-bottom: 1rem; }
.week-row { display: grid; grid-template-columns: 7.5rem repeat(4, minmax(0, 1fr)); gap: 0.4rem; }
.week-head span { font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); font-weight: 600; padding: 0 0.5rem; }
.week-day { display: flex; flex-direction: column; justify-content: center; gap: 0.1rem; padding: 0.5rem; }
.week-day .badge { align-self: flex-start; }
.week-row.today .week-cell { border-color: var(--accent); }
.week-cell {
  position: relative; background: var(--surface); border: 1px solid var(--line); border-radius: 10px;
  padding: 0.45rem 0.55rem 1.6rem; min-height: 4.2rem; font-size: 0.92rem;
}
.week-cell.empty { background: transparent; border-style: dashed; }
.meal-label { display: none; }
.menu-item + .menu-item { margin-top: 0.35rem; padding-top: 0.35rem; border-top: 1px dashed var(--line); }
.menu-item a { color: var(--fg); font-weight: 600; text-decoration: none; }
.menu-item a:hover { color: var(--accent); }
.item-edit summary { color: var(--muted); font-size: 0.8rem; }
.item-edit[open] { background: var(--surface-2); border-radius: 8px; padding: 0.4rem; margin-top: 0.25rem; }
.item-edit input { width: 4.5rem; padding: 0.3rem; }
.item-edit form + form { margin-top: 0.4rem; }
.add-link {
  position: absolute; right: 0.35rem; bottom: 0.25rem; width: 1.5rem; height: 1.5rem; border-radius: 999px;
  display: grid; place-items: center; text-decoration: none; color: var(--muted); font-weight: 700;
}
.add-link:hover { background: var(--accent); color: var(--accent-ink); }
.add-item-form { display: flex; gap: 0.75rem; flex-wrap: wrap; align-items: flex-end; }
.add-item-form select { width: auto; max-width: 100%; }

/* Inicio */
.home-week h3 { font-size: 0.95rem; margin: 1rem 0 0.4rem; }
.today-meals { list-style: none; padding: 0; margin: 0; display: grid; gap: 0.3rem; }

/* Lista del súper */
.list-progress { display: flex; align-items: center; gap: 0.5rem; font-weight: 700; }
.list-progress progress { width: 7rem; }
progress { accent-color: var(--accent); height: 0.6rem; }
.list-actions { margin-bottom: 0.5rem; }
.list-store h2 { margin: 1.25rem 0 0.5rem; }
.list-aisle { padding: 0.6rem 0.9rem; margin-bottom: 0.6rem; }
.list-aisle h3 { margin: 0.1rem 0 0.3rem; font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); }
.list-items { list-style: none; margin: 0; padding: 0; }
.list-item { display: flex; align-items: center; gap: 0.3rem; border-bottom: 1px solid var(--line); scroll-margin-top: 6rem; }
.list-item:last-child { border-bottom: 0; }
.item-toggle { flex: 1; min-width: 0; }
button.check {
  width: 100%; background: none; color: var(--fg); border: 0; padding: 0.65rem 0.1rem; justify-content: flex-start;
  gap: 0.75rem; font-weight: 400; text-align: left; border-radius: 0;
}
button.check:hover { filter: none; background: var(--surface-2); }
.check .box {
  flex: none; width: 1.5rem; height: 1.5rem; border-radius: 6px; border: 2px solid var(--line);
  display: grid; place-items: center; color: transparent; font-size: 0.95rem; font-weight: 800;
}
.item-text { display: flex; flex-wrap: wrap; align-items: baseline; gap: 0 0.6rem; min-width: 0; }
.item-label { font-size: 1.05rem; }
.item-amount { color: var(--muted); font-weight: 600; font-size: 0.95rem; }
.list-item.checked .box { background: var(--ok); border-color: var(--ok); color: #fff; }
.list-item.checked .item-label, .list-item.checked .item-amount { text-decoration: line-through; opacity: 0.5; }
.item-more { flex: none; }
.item-more summary { list-style: none; padding: 0.4rem 0.6rem; color: var(--muted); font-size: 1.2rem; }
.item-more summary::-webkit-details-marker { display: none; }
.item-more[open] {
  position: absolute; right: 1.5rem; z-index: 6; background: var(--surface); border: 1px solid var(--line);
  border-radius: 10px; padding: 0.4rem 0.75rem 0.75rem; box-shadow: var(--shadow); max-width: 20rem;
}
.list-aisle { position: relative; }
#export-text { font-family: ui-monospace, Menlo, monospace; font-size: 0.85rem; margin: 0.5rem 0; }

@media print {
  .topbar, .no-print, .sticky-actions { display: none !important; }
  body { background: #fff; color: #000; }
  .card { box-shadow: none; }
  .recipe-ingredients { position: static; }
}

.center-screen { min-height: 100vh; display: grid; place-items: center; padding: 1.5rem; }

/* Móvil */
@media (max-width: 640px) {
  .topbar { display: grid; grid-template-columns: 1fr auto; gap: 0.5rem; padding: 0.6rem 0.9rem; }
  .nav {
    grid-column: 1 / -1; grid-row: 2; flex-wrap: nowrap; overflow-x: auto;
    margin: 0 -0.9rem; padding: 0 0.9rem; scrollbar-width: none;
  }
  .nav a, .nav .soon { white-space: nowrap; }
  .container { padding: 1.25rem 0.9rem 3rem; }

  /* Tablas → tarjetas apiladas */
  table.stack-mobile thead { display: none; }
  table.stack-mobile, table.stack-mobile tbody, table.stack-mobile tr, table.stack-mobile td { display: block; width: 100%; }
  table.stack-mobile tr { padding: 0.75rem 0; border-bottom: 1px solid var(--line); }
  table.stack-mobile tr:last-child { border-bottom: 0; }
  table.stack-mobile td { border: 0; padding: 0.3rem 0; }
  table.stack-mobile td[data-label]::before {
    content: attr(data-label); display: block; font-size: 0.75rem; text-transform: uppercase;
    letter-spacing: 0.04em; color: var(--muted);
  }
  .card.table-wrap { padding: 0.25rem 1rem; }

  .form-grid { grid-template-columns: 1fr; }
  .filters > * { width: 100%; }
  .filters select { width: 100%; }
  .col-order, .col-num { width: auto; }

  .mobile-only { display: inline; }
  .ing-head { display: none; }
  .ing-row {
    grid-template-columns: 5rem 1fr; padding: 0.6rem 0; border-bottom: 1px solid var(--line);
  }
  .ing-row > input[name$='[ingredient]'] { grid-column: 1 / -1; grid-row: 1; }
  .ing-opt { justify-content: flex-start; gap: 0.4rem; font-size: 0.85rem; }
  .recipe-body { grid-template-columns: 1fr; }
  .recipe-ingredients { position: static; }
  .recipe-grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 0.75rem; }
  .recipe-card-body { padding: 0.6rem 0.75rem 0.75rem; }
  .row.three { flex-wrap: wrap; }

  /* Menú: un bloque por día */
  .week-head { display: none; }
  .week-row { grid-template-columns: 1fr 1fr; padding: 0.6rem 0; border-bottom: 1px solid var(--line); }
  .week-day { grid-column: 1 / -1; flex-direction: row; align-items: baseline; gap: 0.5rem; padding: 0 0 0.2rem; }
  .meal-label { display: block; font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
  .week-cell { min-height: 3.6rem; }
  .week-cell.empty { min-height: 2.3rem; padding-bottom: 0.45rem; }
  .add-item-form > * { width: 100%; }
  .add-item-form select { width: 100%; }

  /* Listas compactas: los datos secundarios van en un solo renglón (“Abarrotes · gramo”). */
  table.compact-mobile td.meta { display: inline; padding: 0; color: var(--muted); font-size: 0.9rem; }
  table.compact-mobile td.meta::before { display: none !important; }
  table.compact-mobile td.meta.empty { display: none; }
  table.compact-mobile td.meta:not(.empty) ~ td.meta:not(.empty)::before {
    content: ' · ' !important; display: inline !important; text-transform: none; letter-spacing: 0; font-size: inherit;
  }
  table.compact-mobile tr { padding: 0.6rem 0; }
}

/* Tokens MCP */
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.new-token { border-color: var(--accent); margin-bottom: 1rem; }
.new-token h4 { margin: 1rem 0 0.4rem; }
.snippet { margin-top: 0.5rem; }
.snippet textarea { font-family: ui-monospace, Menlo, monospace; font-size: 0.8rem; margin: 0.4rem 0; white-space: pre; }

/* Importar receta */
.import-banner { border-color: var(--accent); }
.import-banner p { margin: 0 0 0.6rem; }
.import-photo { display: flex; gap: 0.9rem; align-items: center; font-weight: 500; margin: 0; cursor: pointer; }
.import-photo img { width: 140px; aspect-ratio: 4 / 3; object-fit: cover; border-radius: 8px; }
.import-photo input { width: auto; }
.ing-hint { grid-column: 1 / -1; margin: -0.15rem 0 0.25rem; font-size: 0.78rem; color: var(--muted); }
.badge.new { background: var(--ok-bg); color: var(--ok); }

/* Conectar asistentes (MCP) */
.mcp-connect .row.nowrap input { flex: 1; min-width: 0; }
.mcp-connect .field-hint code { font-size: 0.85em; word-break: break-all; }

/* Botones que dependen de APIs del navegador: se muestran solo si existen (ver enhance.ts). */
[data-copy], [data-share] { display: none; }
.can-copy [data-copy], .can-share [data-share] { display: inline-flex; }
