:root {
  --bg: #FBF8F3;
  --surface: #FFFFFF;
  --surface-2: #F3EEE6;
  --text: #1C1A17;
  --muted: #7A736A;
  --line: #E7E0D5;
  --accent: #E8870E;
  --accent-soft: #FCEBD2;
  --accent-text: #FFFFFF;
  --good: #2E9E5B;
  --warn: #C27A00;
  --warn-soft: #FFF3D6;
  --danger: #D93A31;
  --info-soft: #E6F0FB;
  --info: #2468B3;
  --chart-ma: #1C1A17;
  --radius: 18px;
  --shadow: 0 1px 2px rgba(40, 30, 10, .05), 0 4px 16px rgba(40, 30, 10, .05);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141210;
    --surface: #1F1C19;
    --surface-2: #2A2622;
    --text: #F4EFE8;
    --muted: #A39B90;
    --line: #34302B;
    --accent: #F59A2A;
    --accent-soft: #3A2A14;
    --accent-text: #1C1204;
    --good: #4CC47F;
    --warn: #F0B340;
    --warn-soft: #362A12;
    --danger: #FF6259;
    --info-soft: #172638;
    --info: #7DB5F0;
    --chart-ma: #F4EFE8;
    --shadow: none;
  }
}

* { box-sizing: border-box; }

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 17px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  padding-bottom: calc(84px + env(safe-area-inset-bottom));
}

.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(14px + env(safe-area-inset-top)) 16px 6px;
  max-width: 640px;
  margin: 0 auto;
}

.topbar h1 { font-size: 32px; font-weight: 750; letter-spacing: -.02em; margin: 0; }

.badge {
  font-size: 12px;
  font-weight: 600;
  padding: 3px 9px;
  border-radius: 99px;
  background: var(--accent-soft);
  color: var(--accent);
}

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

.view { display: flex; flex-direction: column; gap: 12px; }
.view[hidden] { display: none; }

.card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
}

h2 { font-size: 19px; margin: 0; }

.label { font-size: 14px; color: var(--muted); }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.big { font-size: 48px; font-weight: 750; letter-spacing: -.03em; line-height: 1.05; font-variant-numeric: tabular-nums; }
.big small { font-size: 20px; font-weight: 600; color: var(--muted); }
.mid { font-size: 26px; font-weight: 700; letter-spacing: -.01em; font-variant-numeric: tabular-nums; }

.hero { flex-direction: row; justify-content: space-between; align-items: flex-end; }
.hero-main, .hero-side { display: flex; flex-direction: column; gap: 2px; }
.hero-side { align-items: flex-end; }

.chip {
  font-size: 13px;
  font-weight: 600;
  padding: 3px 10px;
  border-radius: 99px;
  background: var(--surface-2);
  color: var(--text);
}
.chip.ok { background: color-mix(in srgb, var(--good) 16%, transparent); color: var(--good); }
.chip.warn { background: var(--warn-soft); color: var(--warn); }
.chip.bad { background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); }

.row-between { display: flex; justify-content: space-between; align-items: center; gap: 12px; }

.progress { height: 12px; border-radius: 99px; background: var(--surface-2); overflow: hidden; margin: 6px 0 8px; }
.progress div { height: 100%; width: 0; background: var(--accent); border-radius: 99px; transition: width .5s ease; }

.stats2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.stats2 > div:not(.card) { display: flex; flex-direction: column; }
.tile { gap: 0; }

.btn {
  appearance: none;
  border: 0;
  font: inherit;
  font-weight: 600;
  padding: 12px 18px;
  border-radius: 14px;
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
  text-align: center;
  text-decoration: none;
}
.btn.primary { background: var(--accent); color: var(--accent-text); }
.btn:active { transform: scale(.98); }
.big-btn { display: block; width: 100%; font-size: 19px; padding: 18px; border-radius: 18px; }
.danger-fill { background: var(--danger); color: #fff; }

.alert {
  border-radius: var(--radius);
  padding: 14px 16px;
  font-size: 15px;
  display: flex;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0;
}
#alerts { display: flex; flex-direction: column; gap: 10px; }
#alerts:empty { display: none; }
.alert.warn { background: var(--warn-soft); color: var(--text); }
.alert.info { background: var(--info-soft); color: var(--text); }
.alert.accent { background: var(--accent-soft); color: var(--text); }
.alert button { flex-shrink: 0; padding: 8px 14px; font-size: 14px; background: var(--surface); }

/* Formularios */
.form { gap: 14px; }
.form label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; font-weight: 600; color: var(--muted); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 12px; }
.opt { font-weight: 400; font-size: 12px; }

input, select, textarea {
  font: inherit;
  font-size: 17px;
  color: var(--text);
  background: var(--surface-2);
  border: 1.5px solid transparent;
  border-radius: 12px;
  padding: 12px;
  width: 100%;
  min-width: 0;
  min-height: 48px;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); }
input[aria-invalid="true"] { border-color: var(--danger); }
textarea { resize: vertical; }

.note { margin: -6px 0 0; font-size: 14px; color: var(--info); }
.error { margin: 0; font-size: 14px; color: var(--danger); }

/* Progreso */
.segmented {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  background: var(--surface-2);
  border-radius: 12px;
  padding: 3px;
}
.segmented button {
  appearance: none;
  border: 0;
  background: transparent;
  font: inherit;
  font-size: 15px;
  font-weight: 600;
  color: var(--muted);
  padding: 9px;
  border-radius: 10px;
  cursor: pointer;
}
.segmented button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

.legend { display: flex; gap: 10px; flex-wrap: wrap; justify-content: flex-end; font-size: 12px; color: var(--muted); }
.legend span { display: inline-flex; align-items: center; gap: 4px; }
.sw { display: inline-block; width: 12px; height: 3px; border-radius: 2px; }
.sw-w { background: var(--accent); }
.sw-ma { background: var(--chart-ma); }
.sw-goal { background: repeating-linear-gradient(90deg, var(--good) 0 3px, transparent 3px 5px); }
.sw-bar { height: 10px; width: 8px; background: var(--accent); opacity: .75; }

.chart { width: 100%; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .grid line { stroke: var(--line); }
.chart text { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .empty { fill: var(--muted); font-size: 14px; }

.weeks { width: 100%; border-collapse: collapse; font-size: 14px; font-variant-numeric: tabular-nums; }
.weeks th { text-align: right; font-weight: 600; color: var(--muted); font-size: 12px; padding: 6px 4px; }
.weeks td { text-align: right; padding: 9px 4px; border-top: 1px solid var(--line); }
.weeks th:first-child, .weeks td:first-child { text-align: left; }
.down { color: var(--good); }
.up { color: var(--danger); }

/* Historial */
.list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.list li {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px 16px;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 4px 12px;
  align-items: center;
}
.list .date { font-size: 13px; color: var(--muted); }
.list .w { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }
.list .meta { grid-column: 1 / -1; font-size: 14px; color: var(--muted); display: flex; flex-wrap: wrap; gap: 4px 12px; }
.list .notes { grid-column: 1 / -1; font-size: 14px; white-space: pre-wrap; overflow-wrap: anywhere; }
.list .actions { display: flex; gap: 6px; grid-row: 1 / 3; grid-column: 2; }
.icon-btn {
  appearance: none;
  border: 0;
  background: var(--surface-2);
  color: var(--text);
  width: 44px;
  height: 44px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  cursor: pointer;
}
.icon-btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn.danger { color: var(--danger); }
.empty-state { text-align: center; color: var(--muted); padding: 40px 16px; }

/* Ajustes */
.calc { gap: 10px; font-size: 16px; }
.rows { padding: 4px 0; gap: 0; }
.row-btn {
  appearance: none;
  border: 0;
  background: none;
  font: inherit;
  color: var(--accent);
  font-weight: 600;
  text-align: left;
  padding: 14px 16px;
  min-height: 52px;
  cursor: pointer;
  display: flex;
  align-items: center;
}
.rows > * + * { border-top: 1px solid var(--line); }
.row-btn.danger { color: var(--danger); }
.switch-row { display: flex; justify-content: space-between; align-items: center; gap: 16px; padding: 12px 16px; cursor: pointer; }
.switch-row span { display: flex; flex-direction: column; gap: 2px; }
.switch-row small { color: var(--muted); font-size: 13px; }
.switch-row input {
  appearance: none;
  flex-shrink: 0;
  width: 52px;
  min-height: 0;
  height: 32px;
  padding: 0;
  border: 0;
  border-radius: 99px;
  background: var(--line);
  position: relative;
  cursor: pointer;
  transition: background .2s;
}
.switch-row input::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .25);
  transition: transform .2s;
}
.switch-row input:checked { background: var(--good); }
.switch-row input:checked::after { transform: translateX(20px); }

/* Tab bar */
.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  border-top: 1px solid var(--line);
  z-index: 10;
}
.tabbar a {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 6px 0;
  font-size: 11px;
  font-weight: 600;
  color: var(--muted);
  text-decoration: none;
}
.tabbar svg { width: 26px; height: 26px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tabbar a.active { color: var(--accent); }

/* Diálogo y toast */
dialog {
  border: 0;
  border-radius: 20px;
  padding: 20px;
  width: min(360px, calc(100% - 32px));
  background: var(--surface);
  color: var(--text);
}
dialog::backdrop { background: rgba(0, 0, 0, .4); }
dialog p { margin: 0 0 18px; font-size: 17px; }
.dialog-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

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

@media (max-width: 360px) {
  .grid2 { grid-template-columns: 1fr; }
  .big { font-size: 40px; }
}
