/* ============================================================
   Estilo: minimalista tipo iOS, tonos pastel (crema, marrón, salvia)
   ============================================================ */

:root {
  --bg: #f6f2ec;           /* crema */
  --card: #fffdf9;
  --card2: #f1ebe2;        /* campos de texto */
  --line: #e8e0d5;
  --text: #3d342c;         /* marrón oscuro */
  --muted: #9b8f82;
  --accent: #7d9a74;       /* verde salvia */
  --accent-strong: #66825e;
  --accent-soft: #e4ecdc;
  --brown: #b08f72;        /* marrón latte */
  --brown-soft: #f1e6da;
  --ok: #8fb085;
  --ok-soft: #e7f0e0;
  --danger: #c4776f;       /* rosa palo */
  --gold: #e6c98b;
  --on-accent: #ffffff;    /* texto sobre verde */
  --on-brown: #ffffff;     /* texto sobre marrón */
  --note-text: var(--text);
  --chip-text: var(--text);
  --shadow: 0 1px 2px rgba(80, 60, 40, 0.05), 0 4px 14px rgba(80, 60, 40, 0.05);
  --radius: 18px;
  --tabbar-h: 58px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  /* Paleta "Grafito": gris casi negro neutro (como iOS) y el color solo en los detalles.
     Los pastel llevan texto oscuro encima para que se lean bien. */
  :root {
    --bg: #0e0e0d;
    --card: #1c1b1a;
    --card2: #2a2927;
    --line: #34322f;
    --text: #f3f0eb;
    --muted: #9e9a93;
    --accent: #a8c69e;
    --accent-strong: #b6d2ac;
    --accent-soft: rgba(168, 198, 158, 0.14);
    --brown: #d8bc9b;
    --brown-soft: rgba(216, 188, 155, 0.13);
    --ok: #a8c69e;
    --ok-soft: rgba(168, 198, 158, 0.14);
    --danger: #e0958c;
    --gold: #d9bb7c;
    --on-accent: #17210f;
    --on-brown: #2a1d10;
    --note-text: #e8d6c2;
    --chip-text: #d8bc9b;
    --shadow: none;
    color-scheme: dark;
  }
  input::placeholder, textarea::placeholder { color: #6f6b65; }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; padding: 0; }
button:disabled { opacity: 0.4; }
input, select {
  background: var(--card2);
  border: 1px solid transparent;
  border-radius: 12px;
  padding: 11px 12px;
  width: 100%;
  outline: none;
  -webkit-appearance: none;
  appearance: none;
}
select {
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 16px) 50%, calc(100% - 11px) 50%;
  background-size: 5px 5px;
  background-repeat: no-repeat;
  padding-right: 28px;
}
input:focus, select:focus { border-color: var(--accent); background-color: var(--card); }
input::placeholder, textarea::placeholder { color: #b9ada0; }

.hidden { display: none !important; }

/* ---------- Cabecera (título grande tipo iOS) ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: flex-end; justify-content: space-between; gap: 12px;
  padding: calc(var(--safe-t) + 18px) 20px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(18px); -webkit-backdrop-filter: saturate(1.4) blur(18px);
}
.topbar h1 { margin: 0; font-size: 32px; font-weight: 700; letter-spacing: -0.025em; line-height: 1.1; }
.topbar .sub { color: var(--muted); font-size: 15px; font-variant-numeric: tabular-nums; padding-bottom: 4px; }

main {
  padding: 8px 16px calc(var(--tabbar-h) + var(--safe-b) + 110px);
  max-width: 640px; margin: 0 auto;
}

/* ---------- Barra de pestañas ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: grid; grid-template-columns: repeat(5, 1fr);
  height: calc(var(--tabbar-h) + var(--safe-b));
  padding-bottom: var(--safe-b);
  background: color-mix(in srgb, var(--card) 85%, transparent);
  backdrop-filter: saturate(1.4) blur(18px); -webkit-backdrop-filter: saturate(1.4) blur(18px);
  border-top: 0.5px solid var(--line);
}
.tabbar button {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  color: var(--muted); font-size: 10.5px; font-weight: 600; letter-spacing: 0.01em;
}
.tabbar svg { width: 25px; height: 25px; }
.tabbar button.active { color: var(--accent-strong); }

/* ---------- Tarjetas y elementos comunes ---------- */
.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
  border: 0;
  display: block;
}
.card h3 { margin: 0 0 3px; font-size: 17px; font-weight: 650; letter-spacing: -0.01em; }
.muted { color: var(--muted); }
.small { font-size: 13.5px; }
.section-title {
  font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em;
  color: var(--muted); margin: 26px 6px 8px;
}
.section-title:first-child { margin-top: 6px; }
.row { display: flex; align-items: center; gap: 10px; }
.row.between { justify-content: space-between; }
.grow { flex: 1; min-width: 0; }
.stack > * + * { margin-top: 10px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 13px 18px; border-radius: 14px;
  background: var(--card); color: var(--text);
  font-weight: 600; font-size: 16px;
  box-shadow: var(--shadow);
  transition: transform 0.1s ease, opacity 0.1s ease;
}
.btn:active { transform: scale(0.97); opacity: 0.85; }
.btn.primary { background: var(--accent); color: var(--on-accent); box-shadow: none; }
.btn.ok { background: var(--accent); color: var(--on-accent); box-shadow: none; }
.btn.danger { color: var(--danger); }
.btn.ghost { background: none; box-shadow: none; color: var(--muted); }
.btn.ghost.danger { color: var(--danger); }
.btn.block { width: 100%; }
.btn.sm { padding: 9px 14px; font-size: 15px; border-radius: 12px; }
.card .btn:not(.primary):not(.ok):not(.ghost) { background: var(--card2); box-shadow: none; }
.icon-btn {
  width: 36px; height: 36px; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  color: var(--muted); font-size: 17px; flex: none;
}
.icon-btn:active { background: var(--card2); }

.routine-card { display: flex; align-items: center; gap: 12px; }
.routine-card .info { flex: 1; min-width: 0; }
.routine-card .info p { margin: 2px 0 0; color: var(--muted); font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.empty { text-align: center; color: var(--muted); padding: 36px 12px; line-height: 1.45; }
.empty .big { font-size: 40px; margin-bottom: 10px; }

/* ---------- Entreno activo ---------- */
.ex-card .ex-head { display: flex; align-items: flex-start; gap: 4px; margin-bottom: 10px; }
.ex-card .ex-name { font-size: 18px; font-weight: 650; text-align: left; letter-spacing: -0.01em; }
.ex-card .ex-last { font-size: 13px; color: var(--muted); margin-top: 3px; }
.sets-head, .set-row {
  display: grid; grid-template-columns: 28px 1fr 1fr 46px; gap: 8px; align-items: center;
}
.sets-head { font-size: 11.5px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; padding: 0 2px 6px; }
.sets-head span:not(:first-child) { text-align: center; }
.set-row { padding: 4px 2px; border-radius: 14px; transition: background 0.2s ease; }
.set-row .num { text-align: center; font-weight: 600; color: var(--muted); }
.set-row input { text-align: center; padding: 11px 6px; font-variant-numeric: tabular-nums; font-weight: 600; font-size: 17px; }
.set-row .check {
  height: 44px; border-radius: 50%; width: 44px; justify-self: center;
  background: var(--card2); font-size: 18px; color: var(--muted);
  transition: background 0.2s ease, transform 0.1s ease;
}
.set-row .check:active { transform: scale(0.9); }
.set-row.done { background: var(--ok-soft); }
.set-row.done .check { background: var(--ok); color: var(--on-accent); }
.set-row.done input { background: transparent; }
.ex-actions { display: flex; gap: 6px; margin-top: 10px; }
.ex-actions .btn { flex: 1; }

.ex-note {
  display: block; width: 100%; resize: none; overflow: hidden;
  font-size: 14.5px; line-height: 1.4; color: var(--note-text);
  background: var(--brown-soft); border: 0;
  border-radius: 12px; padding: 9px 12px; margin: 0 0 12px; outline: none;
}
.ex-note:placeholder-shown { background: transparent; box-shadow: inset 0 0 0 1px var(--line); }
.ex-note:focus { background: var(--brown-soft); box-shadow: inset 0 0 0 1.5px var(--brown); }
.edit-ex .ex-note { margin: 10px 0 0; }
.sheet .ex-note { margin-bottom: 14px; }
.other-note { font-size: 13px; color: var(--muted); margin: -6px 4px 12px; white-space: pre-wrap; }

.add-ex { display: flex; gap: 8px; }
.add-ex input { flex: 1; background: var(--card); box-shadow: var(--shadow); }
.add-ex input:focus { background: var(--card); }

.pr-badge {
  display: inline-block; font-size: 10.5px; font-weight: 700; color: #6b5420;
  background: var(--gold); border-radius: 6px; padding: 2px 6px; margin-left: 6px; vertical-align: 2px;
}

/* ---------- Correr ---------- */
.run-ico { width: 1.1em; height: 1.1em; vertical-align: -0.18em; color: var(--brown); }
.run-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.run-grid input { text-align: center; font-size: 18px; font-weight: 600; font-variant-numeric: tabular-nums; }
.run-pace { min-height: 20px; margin: 8px 4px 10px; font-size: 14px; font-weight: 600; color: var(--accent-strong); font-variant-numeric: tabular-nums; }
.run-add { margin-top: 10px; }
.run-add .run-ico { color: inherit; }
.cal-day.has.run { background: var(--brown); color: var(--on-brown); }

/* ---------- Ejercicios ---------- */
.ex-row { display: flex; align-items: center; gap: 10px; }
.ex-row h3 { font-size: 16.5px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.best-card { display: flex; flex-wrap: wrap; gap: 22px; }
.big-num { font-size: 28px; font-weight: 700; letter-spacing: -0.02em; color: var(--accent-strong); }

/* ---------- Gimnasios ---------- */
.gym-chip {
  display: inline-flex; align-items: center; gap: 6px; margin: 2px 0 14px;
  padding: 7px 14px; border-radius: 999px;
  background: var(--brown-soft); color: var(--chip-text);
  font-weight: 600; font-size: 14.5px;
}
.pin { width: 1.05em; height: 1.05em; vertical-align: -0.17em; margin-right: 3px; color: var(--brown); flex: none; }
.gym-row { display: flex; align-items: center; gap: 4px; padding: 6px 8px; }
.gym-pick { display: flex; align-items: center; gap: 12px; padding: 10px 6px; text-align: left; }
.gym-pick .radio { width: 22px; height: 22px; border-radius: 50%; border: 2px solid var(--line); flex: none; }
.gym-row.selected .radio { border: 7px solid var(--accent); }
.gym-tag { font-size: 13px; color: var(--muted); margin-top: 2px; }
.card.dim { opacity: 0.5; }

/* ---------- Calendario ---------- */
.cal-head { display: flex; align-items: center; justify-content: space-between; margin: 4px 0 14px; }
.cal-head h2 { margin: 0; font-size: 20px; font-weight: 650; }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 6px; background: var(--card); border-radius: var(--radius); padding: 12px; box-shadow: var(--shadow); }
.cal-dow { text-align: center; font-size: 11.5px; font-weight: 600; color: var(--muted); padding-bottom: 4px; }
.cal-day {
  aspect-ratio: 1; border-radius: 50%;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-weight: 500; font-variant-numeric: tabular-nums; font-size: 16px;
}
.cal-day.out { visibility: hidden; }
.cal-day.today { color: var(--accent-strong); font-weight: 700; box-shadow: inset 0 0 0 1.5px var(--accent); }
.cal-day.has { background: var(--accent); color: var(--on-accent); font-weight: 600; }
.cal-day .dot { width: 4px; height: 4px; border-radius: 50%; background: currentColor; opacity: 0.8; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-top: 12px; }
.stat { background: var(--card); border-radius: var(--radius); padding: 14px 8px; text-align: center; box-shadow: var(--shadow); }
.stat b { display: block; font-size: 26px; font-weight: 700; font-variant-numeric: tabular-nums; color: var(--accent-strong); }
.stat span { font-size: 12.5px; color: var(--muted); }

.log-ex { padding: 10px 0; border-top: 0.5px solid var(--line); }
.log-ex:first-of-type { border-top: 0; }
.log-ex .name { font-weight: 600; }
.log-ex .sets { color: var(--muted); font-size: 14px; margin-top: 2px; font-variant-numeric: tabular-nums; }

/* ---------- Editor de rutina ---------- */
.edit-ex { background: var(--bg); border-radius: 16px; padding: 12px; margin-bottom: 10px; }
.edit-ex .grid4 { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; margin-top: 10px; }
.edit-ex .grid4 input { text-align: center; padding: 10px 4px; }
.edit-ex label { font-size: 11.5px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; display: block; margin: 0 0 4px 4px; }
.edit-ex input { background: var(--card); }
.field-label { font-size: 13px; color: var(--muted); font-weight: 600; margin: 0 0 6px 4px; display: block; }

/* ---------- Timer ---------- */
.timerbar {
  position: fixed; z-index: 25; left: 12px; right: 12px;
  bottom: calc(var(--tabbar-h) + var(--safe-b) + 10px);
  max-width: 616px; margin: 0 auto;
  display: flex; align-items: center; gap: 8px;
  background: color-mix(in srgb, var(--card) 92%, transparent);
  backdrop-filter: blur(18px); -webkit-backdrop-filter: blur(18px);
  border-radius: 22px; padding: 10px;
  box-shadow: 0 10px 30px rgba(70, 50, 30, 0.16);
}
.timerbar.finished { background: var(--accent); animation: pulse 0.6s ease-in-out 4; }
.timerbar.finished .timer-label, .timerbar.finished .tbtn, .timerbar.finished .timer-time { color: var(--on-accent); }
.timerbar.finished .tbtn { background: rgba(255, 255, 255, 0.2); }
@keyframes pulse { 50% { transform: scale(1.03); } }
.timer-display { flex: 1; text-align: center; }
.timer-label { font-size: 11px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em; }
.timer-time { font-size: 32px; font-weight: 300; font-variant-numeric: tabular-nums; line-height: 1.1; letter-spacing: -0.02em; }
.timer-progress { height: 4px; background: var(--card2); border-radius: 2px; margin-top: 6px; overflow: hidden; }
#timer-fill { height: 100%; width: 0; background: var(--accent); border-radius: 2px; transition: width 0.25s linear; }
.timerbar.finished #timer-fill { background: var(--on-accent); }
.tbtn {
  min-width: 48px; height: 48px; border-radius: 50%; background: var(--card2);
  font-weight: 600; font-size: 14px; font-variant-numeric: tabular-nums;
}
.tbtn.stop { color: var(--muted); }

.fab {
  position: fixed; z-index: 24; right: 18px;
  bottom: calc(var(--tabbar-h) + var(--safe-b) + 16px);
  width: 56px; height: 56px; border-radius: 50%;
  background: var(--brown); color: var(--on-brown);
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 8px 22px rgba(120, 85, 55, 0.3);
}
.fab svg { width: 26px; height: 26px; }
.preset-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.preset-grid .btn { padding: 18px 0; font-size: 19px; font-weight: 500; background: var(--card2); box-shadow: none; font-variant-numeric: tabular-nums; }

/* ---------- Hoja modal ---------- */
.sheet-backdrop {
  position: fixed; inset: 0; z-index: 40; background: rgba(40, 30, 20, 0.3);
  animation: fade 0.2s ease;
}
.sheet {
  position: fixed; z-index: 41; left: 0; right: 0; bottom: 0;
  max-width: 640px; margin: 0 auto;
  max-height: 90vh; overflow-y: auto; -webkit-overflow-scrolling: touch;
  background: var(--card); border-radius: 24px 24px 0 0;
  padding: 8px 18px calc(var(--safe-b) + 22px);
  animation: up 0.3s cubic-bezier(0.2, 0.8, 0.2, 1);
}
.sheet .card { background: var(--bg); box-shadow: none; }
.sheet .card .btn:not(.primary):not(.ok):not(.ghost) { background: var(--card); }
.sheet input, .sheet select { background-color: var(--bg); }
.sheet input:focus, .sheet select:focus { background-color: var(--bg); }
.sheet .add-ex input { box-shadow: none; }
.sheet .edit-ex input, .sheet .edit-ex input:focus { background-color: var(--card); }
.sheet .btn:not(.primary):not(.ok):not(.ghost) { background: var(--bg); box-shadow: none; }
.sheet-handle { width: 38px; height: 5px; border-radius: 3px; background: var(--line); margin: 4px auto 16px; }
.sheet h2 { margin: 0 0 14px; font-size: 22px; font-weight: 700; letter-spacing: -0.02em; }
@keyframes up { from { transform: translateY(100%); } }
@keyframes fade { from { opacity: 0; } }

.toast {
  position: fixed; z-index: 50; left: 50%; transform: translateX(-50%);
  top: calc(var(--safe-t) + 14px);
  background: color-mix(in srgb, var(--text) 92%, transparent); color: var(--bg);
  padding: 10px 18px; border-radius: 999px; font-weight: 600; font-size: 14px;
  box-shadow: 0 8px 24px rgba(40, 30, 20, 0.2);
  animation: fade 0.2s ease;
  max-width: calc(100% - 32px); text-align: center;
}

.history-item { padding: 10px 0; border-top: 0.5px solid var(--line); }
.history-item:first-of-type { border-top: 0; }
