/* Siempre en lila claro, aunque el dispositivo esté en modo oscuro. */
:root {
  --bg: #efe6fc;
  --surface: #ffffff;
  --surface-2: #f5effd;
  --text: #1f1830;
  --muted: #675d78;
  --border: #ddd0f2;
  --accent: #7e57c2;
  --accent-text: #ffffff;
  --accent-soft: #ece3fa;
  --gym: #2563eb;
  --mat: #db2777;
  --sport: #16a34a;
  --food: #d97706;
  --danger: #b42318;
  --danger-soft: #fde7e5;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(0, 0, 0, .06);
  color-scheme: light;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(76px + env(safe-area-inset-bottom));
}

h1, h2, h3 { line-height: 1.2; margin: 0; }
h1 { font-size: 1.15rem; }
h2 { font-size: 1.05rem; margin: 20px 0 10px; }
.card > h2:first-child { margin-top: 0; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  padding: calc(12px + env(safe-area-inset-top)) 16px 12px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}

main { max-width: 720px; margin: 0 auto; padding: 8px 16px 24px; }

.muted { color: var(--muted); }
.small { font-size: .85rem; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin: 12px 0;
  box-shadow: var(--shadow);
}

.field { display: block; margin: 10px 0; font-size: .9rem; color: var(--muted); }
.field input, .field select, .field textarea { margin-top: 4px; }

input, select, textarea {
  width: 100%;
  font: inherit;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  min-height: 44px;
}
textarea { resize: vertical; }
input[type="range"] { padding: 0; background: none; border: 0; accent-color: var(--accent); }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.row2 .field { margin: 10px 0; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font: inherit;
  font-weight: 600;
  border-radius: 10px;
  border: 1px solid transparent;
  padding: 10px 16px;
  min-height: 44px;
  cursor: pointer;
}
.btn.primary { background: var(--accent); color: var(--accent-text); width: 100%; margin-top: 8px; }
.btn.secondary { background: var(--accent-soft); color: var(--text); }
.btn.small { min-height: 36px; padding: 6px 12px; font-size: .9rem; background: var(--surface-2); color: var(--text); border-color: var(--border); }
.btn.danger { background: var(--danger-soft); color: var(--danger); border-color: var(--danger); }
.btn-row { display: flex; flex-wrap: wrap; gap: 10px; }
.btn-row .btn.primary { width: auto; margin-top: 0; }
.btn:disabled { opacity: .6; cursor: wait; }

.icon-btn {
  flex: none;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--muted);
  font-size: 1rem;
  cursor: pointer;
}

/* Gimnasio */
.exercise { border: 1px solid var(--border); border-radius: 12px; padding: 10px; margin: 10px 0; }
.exercise-head { display: flex; gap: 8px; margin-bottom: 6px; }
.set-row { display: grid; grid-template-columns: 24px 1fr 1fr 40px; gap: 8px; align-items: center; margin: 6px 0; }
.set-labels { font-size: .75rem; color: var(--muted); margin-bottom: 0; }
.set-n { text-align: center; color: var(--muted); font-size: .85rem; }
.set-row .icon-btn { width: 40px; height: 40px; }

/* Listas */
.entry { position: relative; }
.entry-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.entry-title { font-weight: 600; }
.entry-actions { display: flex; gap: 6px; flex: none; }
.entry p { margin: 6px 0 0; }
.entry .label { color: var(--muted); font-size: .85rem; }
.ex-line { margin: 4px 0 0; }
.ex-line b { font-weight: 600; }
.badge {
  display: inline-block;
  font-size: .75rem;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--text);
  margin-right: 6px;
}
.empty { color: var(--muted); text-align: center; padding: 20px 8px; }

table { width: 100%; border-collapse: collapse; font-size: .92rem; }
th, td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--border); }
th { color: var(--muted); font-weight: 500; font-size: .8rem; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }

.search { margin: 4px 0 8px; }

/* Resumen */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; margin: 12px 0; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; box-shadow: var(--shadow); }
.stat .k { font-size: .8rem; color: var(--muted); }
.stat .v { font-size: 1.5rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat .s { font-size: .8rem; color: var(--muted); }

.week-strip { display: grid; grid-template-columns: repeat(7, 1fr); gap: 6px; }
.day {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 8px 2px;
  text-align: center;
  font-size: .8rem;
  min-height: 74px;
}
.day.today { border-color: var(--accent); border-width: 2px; }
.day .n { font-weight: 700; font-size: 1rem; }
.dots { display: flex; justify-content: center; gap: 3px; margin-top: 6px; }
.dot { width: 8px; height: 8px; border-radius: 50%; }
.dot.gym { background: var(--gym); }
.dot.mat { background: var(--mat); }
.dot.food { background: var(--food); }
.dot.sport { background: var(--sport); }
.badge.sport { background: #dcfce7; }
.week-nav { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin: 16px 0 10px; }
.week-nav h2 { margin: 0; text-align: center; flex: 1; }
.week-nav .icon-btn:disabled { opacity: .35; cursor: default; }
button.day { font: inherit; color: var(--text); cursor: pointer; width: 100%; }
.day.selected { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.day.selected .muted { color: var(--accent-text); }
#day-detail h3 { margin: 0 0 6px; font-size: 1rem; }
.detail-item { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--border); }
.detail-item:last-child { border-bottom: 0; }
.detail-item .meta { font-size: .85rem; color: var(--muted); }
#mat-cancel { width: 100%; margin-top: 8px; }
#mat-existing-photos .thumb.removed img { opacity: .3; }
#mat-existing-photos .thumbs:empty { display: none; }
.legend { display: flex; flex-wrap: wrap; gap: 12px; font-size: .8rem; color: var(--muted); margin-top: 8px; }
.legend span { display: inline-flex; align-items: center; gap: 4px; }

/* Faixas */
.belt svg { display: block; width: 100%; max-width: 340px; height: auto; margin: 4px 0 8px; }
.belt-name { font-weight: 700; font-size: 1.05rem; margin: 0; }
.belt p { margin: 2px 0; }
#belt-details { margin-top: 12px; }
#belt-details summary { list-style: none; display: inline-flex; }
#belt-details summary::-webkit-details-marker { display: none; }
#belt-details[open] summary { margin-bottom: 4px; }
#belt-preview svg { max-width: 220px; }
#belt-cancel { width: 100%; margin-top: 8px; }
.belt-mini { display: flex; gap: 14px; align-items: center; }
.belt-mini svg { width: 120px; flex: none; }
.grad-item { display: flex; align-items: center; gap: 12px; padding: 8px 0; border-bottom: 1px solid var(--border); }
.grad-item:last-child { border-bottom: 0; }
.grad-item svg { width: 84px; flex: none; }
.grad-item .grow { flex: 1; min-width: 0; }

/* Fotos */
.photo-picker .thumbs { display: flex; flex-wrap: wrap; gap: 10px; margin: 6px 0 8px; }
.photo-picker .thumbs:empty { display: none; }
.photo-picker .btn { margin-top: 4px; }
.thumb { position: relative; width: 72px; height: 72px; }
.thumb img { width: 100%; height: 100%; object-fit: cover; border-radius: 10px; display: block; }
.thumb-x {
  position: absolute; top: -6px; right: -6px;
  width: 26px; height: 26px; border-radius: 50%;
  border: 0; background: var(--text); color: var(--surface);
  font-size: .75rem; cursor: pointer;
}
.photo-strip { display: flex; gap: 8px; margin-top: 10px; overflow-x: auto; }
.photo-thumb { flex: none; width: 84px; height: 84px; padding: 0; border: 0; border-radius: 10px; overflow: hidden; background: var(--surface-2); cursor: pointer; }
.photo-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.viewer {
  position: fixed; inset: 0; z-index: 30;
  display: flex; align-items: center; justify-content: center;
  padding: 16px; background: rgba(20, 12, 34, .92);
}
.viewer img { max-width: 100%; max-height: 100%; border-radius: 8px; }
.viewer-close { position: absolute; top: calc(12px + env(safe-area-inset-top)); right: 12px; background: var(--surface); color: var(--text); }

/* Búsqueda de alimentos y escáner */
.search-row { display: flex; gap: 8px; margin-top: 4px; }
.search-row input { flex: 1; min-width: 0; margin-top: 0; }
.search-row .icon-btn { width: 44px; height: 44px; }
#food-results:empty { display: none; }
#food-results { margin: 6px 0 4px; border: 1px solid var(--border); border-radius: 12px; overflow: hidden; }
.result {
  display: block; width: 100%; text-align: left; font: inherit; color: var(--text);
  background: var(--surface); border: 0; border-bottom: 1px solid var(--border);
  padding: 10px 12px; cursor: pointer;
}
.result:last-child { border-bottom: 0; }
.result .meta { font-size: .8rem; color: var(--muted); }
.results-note { font-size: .8rem; color: var(--muted); padding: 8px 12px; margin: 0; background: var(--surface-2); }
.per100 { background: var(--surface-2); border-radius: 10px; padding: 8px 12px; margin: 6px 0; }
.per100 p { margin: 4px 0 0; }
.scanner-box {
  background: var(--surface); color: var(--text); border-radius: 14px; padding: 14px;
  width: 100%; max-width: 420px; display: flex; flex-direction: column; gap: 10px;
}
.scanner-box video { width: 100%; aspect-ratio: 4 / 3; object-fit: cover; border-radius: 10px; background: #000; }

/* Comida */
.day-nav { display: flex; gap: 8px; align-items: center; padding: 10px; }
.day-nav input { text-align: center; }
.progress { margin: 10px 0; }
.progress-top { display: flex; justify-content: space-between; font-size: .9rem; }
.bar { height: 10px; border-radius: 999px; background: var(--surface-2); overflow: hidden; margin-top: 4px; }
.bar > i { display: block; height: 100%; background: var(--accent); border-radius: 999px; }
.bar.over > i { background: var(--food); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  font: inherit;
  font-size: .85rem;
  padding: 6px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
  min-height: 36px;
}
.meal-group h3 { font-size: .9rem; color: var(--muted); margin: 14px 0 6px; font-weight: 600; }
.food-item { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--border); }
.food-item:last-child { border-bottom: 0; }
.food-item .meta { font-size: .85rem; color: var(--muted); }

/* Peso */
.chart svg { width: 100%; height: auto; display: block; }
.chart .line { fill: none; stroke: var(--accent); stroke-width: 2.5; stroke-linejoin: round; stroke-linecap: round; }
.chart .pt { fill: var(--accent); }
.chart .grid { stroke: var(--border); stroke-width: 1; }
.chart text { fill: var(--muted); font-size: 11px; }

/* Barra inferior */
.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10;
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar button {
  font: inherit;
  font-size: .68rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 0 10px;
  min-height: 60px;
  background: none;
  border: 0;
  color: var(--muted);
  cursor: pointer;
}
.tabbar button span { font-size: 1.3rem; line-height: 1; }
.tabbar button[aria-current="page"] { color: var(--accent); font-weight: 700; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(84px + env(safe-area-inset-bottom));
  transform: translate(-50%, 20px);
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: .9rem;
  max-width: calc(100% - 32px);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 20;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (min-width: 720px) {
  .tabbar { left: 50%; right: auto; width: 720px; transform: translateX(-50%); border: 1px solid var(--border); border-bottom: 0; border-radius: 16px 16px 0 0; }
}
button.day:disabled { opacity: .45; cursor: default; }
