/* ============================================================
   styles.css - Mobile-first, sin librerías externas
   ============================================================ */

:root {
  --green: #16a34a;
  --green-dark: #15803d;
  --yellow: #d97706;
  --red: #dc2626;
  --bg: #f6f7f4;
  --card: #ffffff;
  --text: #1f2937;
  --muted: #6b7280;
  --border: #e5e7eb;
  --radius: 14px;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  padding-bottom: 76px; /* hueco para la bottomnav */
}

/* ---------- Barra superior ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--green);
  color: #fff;
  padding-top: env(safe-area-inset-top);
}

.topbar-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  max-width: 760px;
  margin: 0 auto;
  padding: 12px 16px;
}

.brand {
  color: #fff;
  text-decoration: none;
  font-weight: 700;
  font-size: 1.1rem;
}

.topbar-status { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }

.chip {
  display: inline-block;
  background: rgba(255, 255, 255, 0.18);
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 0.78rem;
  white-space: nowrap;
}

#active-user-chip { cursor: pointer; }

.chip-cal.macro-green { background: rgba(255, 255, 255, 0.25); }
.chip-cal.macro-yellow { background: var(--yellow); }
.chip-cal.macro-red { background: var(--red); }

/* ---------- Layout ---------- */
.view {
  max-width: 760px;
  margin: 0 auto;
  padding: 16px;
}

h1 { font-size: 1.4rem; margin: 8px 0 16px; }
h2 { font-size: 1.05rem; margin: 0 0 12px; }

.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-bottom: 16px;
}

.muted { color: var(--muted); }
.loading { text-align: center; color: var(--muted); padding: 40px 0; }
.error-card { border-left: 4px solid var(--red); }

.plain-list { list-style: none; margin: 0; padding: 0; }
.plain-list li { padding: 6px 0; border-bottom: 1px solid var(--border); }
.plain-list li:last-child { border-bottom: none; }

.back-link { color: var(--green-dark); text-decoration: none; font-size: 0.9rem; }

/* ---------- Macros con semáforo ---------- */
.macro { margin-bottom: 12px; }
.macro-head { display: flex; justify-content: space-between; font-size: 0.85rem; margin-bottom: 4px; }

.bar { height: 10px; background: var(--border); border-radius: 999px; overflow: hidden; }
.bar-fill { height: 100%; border-radius: 999px; transition: width 0.3s; background: var(--green); }
.macro-yellow .bar-fill { background: var(--yellow); }
.macro-red .bar-fill { background: var(--red); }
.macro-green .macro-head span:last-child { color: var(--green-dark); }
.macro-yellow .macro-head span:last-child { color: var(--yellow); }
.macro-red .macro-head span:last-child { color: var(--red); font-weight: 700; }

/* ---------- Usuarios ---------- */
.user-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}

.user-card {
  background: var(--card);
  border: 2px solid transparent;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  font: inherit;
  color: inherit;
}

.user-card:active { transform: scale(0.97); }
.user-card-active { border-color: var(--green); }
.user-card-new { border: 2px dashed var(--border); background: transparent; box-shadow: none; }

.user-avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: var(--green);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.3rem;
  font-weight: 700;
}

/* ---------- Formularios ---------- */
.form label { display: block; margin-bottom: 10px; font-size: 0.85rem; color: var(--muted); }

.form input, .form select, .select-block, .filters select {
  width: 100%;
  padding: 10px 12px;
  margin-top: 4px;
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 1rem;
  background: #fff;
  color: var(--text);
}

.form-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); gap: 10px; }

.btn {
  display: inline-block;
  border: none;
  border-radius: 10px;
  padding: 12px 18px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  text-align: center;
}

.btn-primary { background: var(--green); color: #fff; width: 100%; }
.btn-primary:active { background: var(--green-dark); }
.btn-ghost { background: transparent; color: var(--green-dark); width: 100%; margin-top: 4px; }

.btn-icon { background: none; border: none; font-size: 1.1rem; cursor: pointer; padding: 6px; }

.select-block { margin-bottom: 16px; }

/* ---------- Autocomplete ---------- */
.autocomplete { position: relative; margin-bottom: 10px; }

.autocomplete input { width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px; font-size: 1rem; }

.autocomplete-results {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  z-index: 10;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 10px;
  list-style: none;
  margin: 4px 0 0;
  padding: 0;
  max-height: 220px;
  overflow-y: auto;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12);
}

.autocomplete-results li { padding: 10px 12px; cursor: pointer; display: flex; justify-content: space-between; }
.autocomplete-results li:hover { background: var(--bg); }
.autocomplete-results li small { color: var(--muted); }

/* ---------- Despensa ---------- */
.pantry-list { list-style: none; margin: 0; padding: 0; }

.pantry-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}

.pantry-item:last-child { border-bottom: none; }
.pantry-item small { display: block; color: var(--muted); }
.pantry-item.expiring small { color: var(--red); font-weight: 600; }
.pantry-actions { display: flex; gap: 2px; }

/* ---------- Recetas ---------- */
.filters { display: flex; gap: 10px; margin-bottom: 16px; }

.recipe-card { display: block; text-decoration: none; color: inherit; }

.recipe-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.recipe-head small { display: block; color: var(--muted); margin-top: 2px; }

.score-badge {
  min-width: 46px;
  height: 46px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 1.05rem;
  color: #fff;
  flex-shrink: 0;
}

.score-high { background: var(--green); }
.score-mid { background: var(--yellow); }
.score-low { background: var(--red); }

.subscores {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  margin-top: 10px;
  font-size: 0.8rem;
  color: var(--muted);
}

.subscores .kcal { margin-left: auto; font-weight: 600; color: var(--text); }

.meta-chips { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 16px; }
.meta-chips .chip { background: var(--card); color: var(--text); box-shadow: var(--shadow); }

.nutri-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  text-align: center;
}

.nutri-grid strong { display: block; font-size: 1.2rem; color: var(--green-dark); }
.nutri-grid small { color: var(--muted); }

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

/* ---------- Histórico: pestañas Día/Semana/Mes ---------- */
.tabs {
  display: flex;
  background: var(--card);
  border-radius: 999px;
  padding: 4px;
  box-shadow: var(--shadow);
  margin-bottom: 16px;
}

.tab {
  flex: 1;
  border: none;
  background: transparent;
  padding: 9px 0;
  border-radius: 999px;
  font: inherit;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
}

.tab.active { background: var(--green); color: #fff; }

.period-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 14px;
}

.period-label {
  flex: 1;
  border: none;
  background: transparent;
  font: inherit;
  font-weight: 700;
  text-align: center;
  cursor: pointer;
  color: var(--text);
}

.meal-row { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.meal-row small { display: block; }

/* Estados semáforo compartidos (día en objetivo / desviado / sin datos) */
.st-ok { background: var(--green); }
.st-under { background: var(--yellow); }
.st-over { background: var(--red); }
.st-logged { background: #94a3b8; }
.st-none { background: var(--border); }

/* ---------- Histórico: gráfico semanal ---------- */
.chart { position: relative; height: 140px; margin-top: 8px; }

.chart-target {
  position: absolute;
  left: 0;
  right: 0;
  border-top: 2px dashed var(--text);
  opacity: 0.35;
  z-index: 1;
}

.chart-bars { display: flex; height: 100%; align-items: flex-end; gap: 6px; }

.chart-col {
  flex: 1;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  align-items: center;
  gap: 4px;
  cursor: pointer;
}

.chart-bar { width: 70%; min-height: 2px; border-radius: 6px 6px 0 0; }
.chart-col small { color: var(--muted); }
.chart-legend { font-size: 0.78rem; margin: 8px 0 0; }

.week-table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
.week-table th { text-align: left; color: var(--muted); font-weight: 600; padding: 6px 4px; }
.week-table td { padding: 8px 4px; border-top: 1px solid var(--border); }
.week-table tr[data-goto-day] { cursor: pointer; }
.week-table tr.muted td { color: var(--muted); }

/* ---------- Histórico: calendario mensual ---------- */
.cal-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 6px;
}

.cal-head { margin-bottom: 6px; text-align: center; color: var(--muted); font-size: 0.75rem; font-weight: 700; }

.cal-day {
  aspect-ratio: 1;
  border: none;
  border-radius: 10px;
  font: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  color: var(--text);
}

.cal-day.st-ok, .cal-day.st-over { color: #fff; }
.cal-day.st-under { color: #fff; }
.cal-day.cal-today { outline: 3px solid var(--green-dark); outline-offset: -1px; }

.cal-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin-top: 12px;
  font-size: 0.75rem;
  color: var(--muted);
}

.cal-legend .dot {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  margin-right: 4px;
}

/* ---------- Navegación inferior ---------- */
.bottomnav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 20;
  display: flex;
  background: var(--card);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}

.bottomnav a {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 0 6px;
  text-decoration: none;
  color: var(--muted);
  font-size: 1.15rem;
}

.bottomnav a span { font-size: 0.65rem; }
.bottomnav a.active { color: var(--green-dark); font-weight: 700; }

/* ---------- Toast y offline ---------- */
.toast {
  position: fixed;
  bottom: 84px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: #fff;
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 0.9rem;
  z-index: 50;
  max-width: 90vw;
  text-align: center;
}

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

.offline-banner {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 30;
  background: var(--yellow);
  color: #fff;
  text-align: center;
  padding: 6px;
  font-size: 0.85rem;
}

/* ---------- Escritorio ---------- */
@media (min-width: 768px) {
  h1 { font-size: 1.7rem; }
  .nutri-grid { grid-template-columns: repeat(6, 1fr); }
}
