:root {
  color-scheme: light;
  --bg: #F2F2F7;
  --card: #FFFFFF;
  --card-2: #F7F7FA;
  --text: #111114;
  --muted: #6C6C72;
  --line: #E3E3E8;
  --accent: #0A84FF;
  --accent-text: #FFFFFF;
  --danger: #FF3B30;
  --ok: #30D158;
  --cell-empty: #E9E9EE;
  --track: rgba(120, 120, 128, 0.16);
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 4px 16px rgba(0, 0, 0, 0.04);
  --radius: 16px;
  --tabbar-h: 64px;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, system-ui, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #000000;
    --card: #1C1C1E;
    --card-2: #2C2C2E;
    --text: #F5F5F7;
    --muted: #9A9AA0;
    --line: #2E2E32;
    --cell-empty: #2A2A2D;
    --track: rgba(120, 120, 128, 0.32);
    --shadow: none;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #000000;
  --card: #1C1C1E;
  --card-2: #2C2C2E;
  --text: #F5F5F7;
  --muted: #9A9AA0;
  --line: #2E2E32;
  --cell-empty: #2A2A2D;
  --track: rgba(120, 120, 128, 0.32);
  --shadow: none;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px);
}

a { color: var(--accent); text-decoration: none; }

button, input, select { font: inherit; color: inherit; }

button { cursor: pointer; }

.container { max-width: 760px; margin: 0 auto; padding: 0 16px; }

/* --- Barra superior y pestañas ------------------------------------------ */

/* Sin backdrop-filter en la barra superior: crearía un contenedor para los
   elementos fijos y la barra de pestañas del móvil dejaría de ir abajo. */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--bg);
  padding-top: env(safe-area-inset-top);
}

@media (min-width: 760px) {
  .topbar {
    background: color-mix(in srgb, var(--bg) 85%, transparent);
    backdrop-filter: saturate(180%) blur(20px);
    -webkit-backdrop-filter: saturate(180%) blur(20px);
  }
}

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

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 20px;
  color: var(--text);
}

.brand img { border-radius: 7px; }

.tabs {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  justify-content: space-around;
  background: color-mix(in srgb, var(--card) 90%, transparent);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-top: 1px solid var(--line);
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
  z-index: 10;
}

.tab {
  flex: 1;
  max-width: 120px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 4px 0;
  font-size: 11px;
  font-weight: 500;
  color: var(--muted);
}

.tab svg { width: 26px; height: 26px; }

.tab[aria-current="page"] { color: var(--accent); }

@media (min-width: 760px) {
  body { padding-bottom: 32px; }
  .tabs {
    position: static;
    background: none;
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
    border: 0;
    padding: 0;
    gap: 4px;
  }
  .tab {
    flex-direction: row;
    gap: 6px;
    font-size: 14px;
    padding: 6px 12px;
    border-radius: 999px;
    max-width: none;
  }
  .tab svg { width: 18px; height: 18px; }
  .tab[aria-current="page"] { background: var(--card); box-shadow: var(--shadow); }
}

/* --- Bloques ---------------------------------------------------------- */

h1 { font-size: 30px; margin: 8px 0 4px; letter-spacing: -0.02em; }
h2 { font-size: 20px; margin: 24px 0 8px; letter-spacing: -0.01em; }
h3 { font-size: 15px; margin: 0; }

.subtitle { color: var(--muted); margin: 0 0 12px; }

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

.list { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }

.section-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  margin: 24px 4px 8px;
}

.section-head h2 { margin: 0; }

.section-head .meta { color: var(--muted); font-size: 14px; }

.muted { color: var(--muted); }

.small { font-size: 13px; }

.row-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 16px; }

/* --- Botones ---------------------------------------------------------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 0 18px;
  border-radius: 12px;
  border: 0;
  background: var(--card-2);
  font-weight: 600;
}

.btn.primary { background: var(--accent); color: var(--accent-text); }
.btn.danger { color: var(--danger); }
.btn.block { width: 100%; }
.btn:disabled { opacity: 0.45; cursor: default; }

.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 0;
  background: transparent;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
}

.icon-btn:hover { background: var(--card-2); }
.icon-btn svg { width: 22px; height: 22px; }
.icon-btn:disabled { opacity: 0.3; cursor: default; background: none; }

/* --- Anillos de resumen --------------------------------------------- */

.rings {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.ring-card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px 6px 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  text-align: center;
}

.ring-card .label { font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; }

.ring { display: block; }
.ring .track { stroke: var(--track); }
.ring text { fill: var(--text); font-weight: 700; }

/* --- Navegación de fecha -------------------------------------------- */

.datebar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 4px 0 16px;
}

.datebar .when { text-align: center; border: 0; background: none; padding: 4px 8px; border-radius: 10px; }
.datebar .when strong { display: block; font-size: 22px; }
.datebar .when span { color: var(--muted); font-size: 14px; }

/* --- Filas de reto -------------------------------------------------- */

.reto {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 12px 12px 14px;
  border-bottom: 1px solid var(--line);
}

.reto:last-child { border-bottom: 0; }

.reto-main {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 12px;
  background: none;
  border: 0;
  padding: 0;
  text-align: left;
}

.badge {
  flex: none;
  width: 42px;
  height: 42px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  font-size: 22px;
  background: color-mix(in srgb, var(--c) 18%, transparent);
}

.reto-text { flex: 1; min-width: 0; }

.reto-name {
  display: block;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.reto-sub { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 13px; margin-top: 2px; }

.bar { display: block; height: 5px; background: var(--track); border-radius: 3px; overflow: hidden; margin-top: 6px; }
.bar > i { display: block; height: 100%; background: var(--c); border-radius: 3px; transition: width 0.25s ease; }

.streak { white-space: nowrap; }

.check {
  flex: none;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 2.5px solid color-mix(in srgb, var(--c) 70%, transparent);
  background: transparent;
  display: grid;
  place-items: center;
  color: transparent;
  transition: transform 0.12s ease, background 0.2s ease;
}

.check svg { width: 24px; height: 24px; }
.check.done { background: var(--c); border-color: var(--c); color: #FFFFFF; }
.check:active { transform: scale(0.92); }

.plus {
  flex: none;
  min-width: 52px;
  height: 40px;
  padding: 0 12px;
  border-radius: 999px;
  border: 0;
  background: color-mix(in srgb, var(--c) 18%, transparent);
  color: var(--text);
  font-weight: 700;
  font-size: 15px;
}

.plus.done { background: var(--c); color: #FFFFFF; }
.plus.big { height: 44px; min-width: 72px; font-size: 17px; }
.plus:active { transform: scale(0.94); }

.busy { opacity: 0.5; pointer-events: none; }

/* --- Vacío y plantillas -------------------------------------------- */

.empty { text-align: center; padding: 28px 16px; }
.empty .big { font-size: 44px; }

.templates {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 8px;
  margin-top: 8px;
}

.template {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: var(--card);
  text-align: left;
}

.template .t-emoji { font-size: 24px; }
.template .t-name { font-weight: 600; font-size: 14px; }
.template .t-meta { font-size: 12px; color: var(--muted); }

/* --- Detalle ---------------------------------------------------------- */

.detail-head { display: flex; align-items: center; gap: 14px; margin: 8px 0 16px; }
.detail-head .badge { width: 56px; height: 56px; font-size: 30px; border-radius: 16px; }
.detail-head h1 { margin: 0; font-size: 26px; }

.hero { display: flex; align-items: center; gap: 20px; }
.hero-text strong { display: block; font-size: 28px; letter-spacing: -0.02em; }

.stat-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 12px; }

.stat {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px;
}

.stat .v { font-size: 22px; font-weight: 700; letter-spacing: -0.02em; }
.stat .k { font-size: 12px; color: var(--muted); }

.chart { width: 100%; height: auto; display: block; }
.chart text { fill: var(--muted); font-size: 10px; }
.chart .grid { stroke: var(--line); }

.heatmap { width: 100%; height: auto; display: block; }
.heatmap text { fill: var(--muted); font-size: 9px; }

.legend { display: flex; justify-content: flex-end; align-items: center; gap: 4px; font-size: 12px; color: var(--muted); margin-top: 6px; }
.legend i { width: 11px; height: 11px; border-radius: 3px; display: inline-block; }

/* --- Estadísticas ------------------------------------------------------ */

.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th { text-align: left; color: var(--muted); font-weight: 500; font-size: 12px; padding: 8px 12px; }
.table td { padding: 10px 12px; border-top: 1px solid var(--line); }
.table td.num, .table th.num { text-align: right; white-space: nowrap; }

/* --- Formularios ------------------------------------------------------ */

.sheet {
  width: min(560px, calc(100vw - 24px));
  max-height: calc(100dvh - 24px);
  border: 0;
  border-radius: 20px;
  padding: 0;
  background: var(--card);
  color: var(--text);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}

.sheet::backdrop { background: rgba(0, 0, 0, 0.4); }

.sheet form { padding: 20px; display: flex; flex-direction: column; gap: 14px; }

.sheet-head { display: flex; align-items: center; justify-content: space-between; }
.sheet-head h2 { margin: 0; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field > span { font-size: 13px; color: var(--muted); font-weight: 600; }

.input {
  width: 100%;
  min-height: 44px;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--card-2);
}

.input:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: transparent; }

.grid-2 { display: grid; grid-template-columns: 90px 1fr; gap: 10px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }

.segmented {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  background: var(--card-2);
  border-radius: 12px;
  padding: 3px;
  gap: 2px;
}

.segmented label { position: relative; }
.segmented input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.segmented span { display: block; text-align: center; padding: 8px 4px; border-radius: 9px; font-size: 14px; font-weight: 600; color: var(--muted); }
.segmented input:checked + span { background: var(--card); color: var(--text); box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12); }
.segmented input:focus-visible + span { outline: 2px solid var(--accent); }

.swatches { display: flex; flex-wrap: wrap; gap: 8px; }
.swatches label { position: relative; }
.swatches input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.swatches span { display: block; width: 32px; height: 32px; border-radius: 50%; background: var(--c); border: 3px solid transparent; }
.swatches input:checked + span { border-color: var(--card); box-shadow: 0 0 0 2px var(--c); }
.swatches input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

.emoji-picks { display: flex; flex-wrap: wrap; gap: 4px; }
.emoji-picks button { border: 0; background: var(--card-2); border-radius: 10px; width: 38px; height: 38px; font-size: 20px; }

.stepper { display: grid; grid-template-columns: 56px 1fr 56px; gap: 8px; align-items: center; }
.stepper .input { text-align: center; font-size: 22px; font-weight: 700; }

.hint { font-size: 13px; color: var(--muted); margin: 0; }

/* --- Avisos ----------------------------------------------------------- */

.toasts {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px);
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 50;
  width: min(420px, calc(100vw - 32px));
}

.toast {
  background: #1C1C1E;
  color: #FFFFFF;
  padding: 12px 16px;
  border-radius: 14px;
  font-size: 14px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
  animation: rise 0.2s ease;
}

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

.banner { background: color-mix(in srgb, var(--danger) 14%, var(--card)); border-radius: 12px; padding: 10px 14px; font-size: 14px; margin-bottom: 12px; }

@keyframes rise { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

.login { max-width: 420px; margin: 10vh auto 0; }
.login .logo { width: 72px; height: 72px; border-radius: 18px; display: block; margin: 0 auto 12px; }
.login h1 { text-align: center; }
.login p { text-align: center; }

.skeleton { height: 64px; border-radius: var(--radius); background: var(--card); margin-bottom: 8px; opacity: 0.6; }

@media (max-width: 420px) {
  .reto { gap: 6px; padding-left: 12px; padding-right: 10px; }
  .reto .icon-btn { width: 32px; height: 32px; }
  .reto-main { gap: 10px; }
  .rings { gap: 6px; }
  .ring-card { padding: 10px 2px 8px; }
  .stat-grid { gap: 6px; }
  .stat .v { font-size: 19px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
