:root {
  --bg: #faf6f0;
  --card: #ffffff;
  --text: #2b2118;
  --muted: #7d6e60;
  --line: #eadfce;
  --accent: #7a4e25;
  --accent-text: #ffffff;
  --me: #fff3dc;
  --shadow: 0 1px 2px rgba(60, 40, 20, .08), 0 4px 14px rgba(60, 40, 20, .06);
  --radius: 16px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17120d;
    --card: #231b14;
    --text: #f3e9dc;
    --muted: #a8998a;
    --line: #3a2e23;
    --accent: #c98b4a;
    --accent-text: #1b1208;
    --me: #3a2b17;
    --shadow: none;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 16px/1.4 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  min-height: 100dvh;
  -webkit-text-size-adjust: 100%;
}

h1, h2 { margin: 0 0 .5rem; line-height: 1.15; }
h2 { font-size: 1.05rem; }
.muted { color: var(--muted); }
.small { font-size: .82rem; font-weight: 400; }
.grow { flex: 1; min-width: 0; }
.empty { text-align: center; margin: .5rem 0; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.error { color: #c0392b; margin: .25rem 0 0; }
.ico { flex: none; display: block; }
@media (prefers-color-scheme: dark) { .ico { filter: brightness(1.4) saturate(1.1); } }

/* ---------- Botones ---------- */
.btn {
  appearance: none; border: 1px solid var(--line); background: var(--card); color: var(--text);
  font: inherit; font-weight: 600; padding: .8rem 1.1rem; border-radius: 12px; cursor: pointer;
  min-height: 48px;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn:active:not(:disabled) { transform: scale(.98); }

.icon-btn {
  appearance: none; border: 0; background: transparent; color: var(--text); font: inherit;
  font-size: 1.6rem; width: 44px; height: 44px; border-radius: 50%; cursor: pointer; line-height: 1;
}
.icon-btn:hover:not(:disabled) { background: var(--line); }
.icon-btn:disabled { opacity: .25; cursor: default; }
.icon-btn.small { font-size: 1rem; width: 36px; height: 36px; }

:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

/* ---------- Login ---------- */
.screen { min-height: 100dvh; }
#login { display: grid; place-items: center; padding: 1.5rem; }
.login-card { width: 100%; max-width: 380px; text-align: center; }
.logo-big { font-size: 4.5rem; line-height: 1; }
.login-card h1 { font-size: 2rem; margin-top: .25rem; }
.login-card form { display: grid; gap: .75rem; margin-top: 1.25rem; }
.login-card form input { width: 100%; }
input[type=text] {
  font: inherit; font-size: 16px; /* 16px evita el zoom automático en iOS */
  padding: .85rem 1rem; border-radius: 12px; border: 1px solid var(--line);
  background: var(--card); color: var(--text); text-align: center;
}

/* ---------- App ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(.6rem + var(--safe-top)) 1rem .6rem;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand { font-weight: 800; font-size: 1.2rem; letter-spacing: -.01em; }
.user-chip {
  appearance: none; border: 1px solid var(--line); background: var(--card); color: var(--text);
  font: inherit; font-size: .9rem; padding: .4rem .8rem; border-radius: 999px; cursor: pointer; max-width: 55vw;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.content {
  max-width: 640px; margin: 0 auto;
  padding: 1rem 1rem calc(6.5rem + var(--safe-bottom));
  display: grid; gap: 1rem;
}

.tabs { display: grid; grid-template-columns: repeat(3, 1fr); background: var(--line); padding: 4px; border-radius: 12px; gap: 4px; }
.tabs button {
  appearance: none; border: 0; background: transparent; color: var(--muted); font: inherit; font-weight: 600;
  padding: .55rem; border-radius: 9px; cursor: pointer;
}
.tabs button[aria-selected=true] { background: var(--card); color: var(--text); box-shadow: var(--shadow); }

.period-nav { display: flex; align-items: center; justify-content: space-between; }
.period-label { font-weight: 700; font-size: 1.15rem; text-align: center; }

.tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: .6rem; }
.tile { background: var(--card); border-radius: var(--radius); padding: .8rem .4rem; text-align: center; box-shadow: var(--shadow); }
.tile-value { font-size: 1.7rem; font-weight: 800; min-height: 2.1rem; display: flex; align-items: center; justify-content: center; gap: .35rem; }
.tile-label { color: var(--muted); font-size: .78rem; }

.card { background: var(--card); border-radius: var(--radius); padding: 1rem; box-shadow: var(--shadow); }

/* Registros del día */
.entries { list-style: none; margin: 0; padding: 0; display: grid; gap: .5rem; }
.entries li { display: flex; align-items: center; gap: .75rem; padding: .5rem .5rem .5rem .75rem; border-radius: 12px; background: var(--bg); }
.entries time { color: var(--muted); font-variant-numeric: tabular-nums; }

/* Barras mes / año */
.bars { display: flex; gap: 2px; align-items: stretch; height: 170px; }
.bar-col { flex: 1; display: flex; flex-direction: column; align-items: center; min-width: 0; }
.bar-track { flex: 1; width: 100%; display: flex; align-items: flex-end; justify-content: center; border-bottom: 1px solid var(--line); }
.bar { width: 78%; max-width: 28px; min-height: 0; background: var(--line); border-radius: 4px 4px 0 0; transition: height .3s; }
.bar-count { font-size: .68rem; height: 1em; color: var(--muted); }
.bar-label { font-size: .62rem; height: 1.2em; color: var(--muted); white-space: nowrap; }
.bars.year .bar-label { font-size: .68rem; }
.bars.month { gap: 1px; }
.bars.month .bar-count { font-size: .55rem; }
.legend { margin: .6rem 0 0; }

/* Distribución */
.dist { display: grid; gap: .45rem; }
.dist-row { display: grid; grid-template-columns: 28px 4.8rem 1fr 2rem; align-items: center; gap: .5rem; }
.dist-name { font-size: .88rem; }
.dist-track { height: 12px; background: var(--bg); border-radius: 6px; overflow: hidden; }
.dist-bar { height: 100%; border-radius: 6px; min-width: 0; transition: width .3s; }
.dist-n { text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; }

/* Ranking */
.ranking { list-style: none; margin: 0; padding: 0; display: grid; gap: .35rem; }
.ranking li { border-radius: 12px; background: var(--bg); }
.rank-head {
  display: flex; align-items: center; gap: .7rem; width: 100%; padding: .6rem .75rem; text-align: left;
  appearance: none; border: 0; background: none; color: inherit; font: inherit; border-radius: 12px;
}
button.rank-head { cursor: pointer; }
.chev { color: var(--muted); transition: transform .2s; }
.rank-head.open .chev { transform: rotate(180deg); }
.rank-detail { padding: 0 .6rem .6rem; }
.entry-list { list-style: none; margin: 0; padding: 0; display: grid; gap: .3rem; }
.entry-list li { background: var(--card); }
.e-btn {
  display: flex; align-items: center; gap: .6rem; width: 100%; padding: .45rem .6rem; text-align: left;
  appearance: none; border: 0; background: none; color: inherit; font: inherit; border-radius: 10px; cursor: pointer;
}
.e-btn time { color: var(--muted); font-size: .82rem; font-variant-numeric: tabular-nums; }
.e-btn.locked { cursor: not-allowed; color: var(--muted); opacity: .75; }
.e-btn.locked .lock { width: 30px; text-align: center; }
.note-box { margin: 0 .6rem .55rem 2.9rem; font-size: .9rem; white-space: pre-wrap; overflow-wrap: anywhere; }

.vis-btn {
  appearance: none; border: 1px solid var(--line); background: var(--card); color: var(--muted); font: inherit;
  font-size: .72rem; padding: .25rem .5rem; border-radius: 999px; cursor: pointer; white-space: nowrap;
}
.entries .vis-btn { margin-top: .35rem; }
.vis-btn.is-public { color: var(--text); border-color: var(--accent); }

.switch-row { display: flex; align-items: center; gap: .8rem; margin: 0 0 .9rem; padding: .6rem .8rem; border-radius: 12px; background: var(--bg); cursor: pointer; }
.switch-row input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch { flex: none; width: 46px; height: 28px; border-radius: 999px; background: var(--line); position: relative; transition: background .2s; }
.switch::after { content: ''; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; transition: transform .2s; box-shadow: 0 1px 3px rgba(0,0,0,.3); }
.switch-row input:checked + .switch { background: var(--accent); }
.switch-row input:checked + .switch::after { transform: translateX(18px); }
.switch-row input:focus-visible + .switch { outline: 3px solid var(--accent); outline-offset: 2px; }
.block { display: block; }
.ranking li.me { background: var(--me); outline: 2px solid var(--accent); }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: .5rem; margin-bottom: .5rem; }
.card-head h2 { margin: 0; }
.seg { display: flex; background: var(--bg); border-radius: 999px; padding: 3px; gap: 2px; }
.seg button { appearance: none; border: 0; background: transparent; color: var(--muted); font: inherit; font-size: .8rem; font-weight: 600; padding: .3rem .7rem; border-radius: 999px; cursor: pointer; }
.seg button[aria-selected=true] { background: var(--accent); color: var(--accent-text); }
.places { list-style: none; margin: 0; padding: 0; display: grid; gap: .55rem; }
.places li { display: flex; align-items: center; gap: .7rem; }
.places .pos { width: 2rem; text-align: center; font-size: 1.3rem; font-weight: 700; }
.place-name { font-weight: 600; margin-bottom: .25rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.places .count { font-size: 1.1rem; min-width: 1.5rem; text-align: right; }
#places-help { margin: .7rem 0 0; }
.ranking li.me .entry-list li { background: var(--bg); }
.ranking .pos { width: 2rem; text-align: center; font-size: 1.35rem; font-weight: 700; }
.ranking .nick { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.ranking .count { font-size: 1.2rem; min-width: 1.5rem; text-align: right; }

/* FAB */
.fab {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(1rem + var(--safe-bottom)); z-index: 6;
  appearance: none; border: 0; background: var(--accent); color: var(--accent-text);
  font: inherit; font-weight: 700; font-size: 1.05rem; padding: .95rem 1.7rem; border-radius: 999px;
  box-shadow: 0 6px 20px rgba(60, 40, 20, .35); cursor: pointer;
}
.fab:active { transform: translateX(-50%) scale(.97); }

/* ---------- Modal ---------- */
.modal {
  position: fixed; inset: 0; z-index: 20; background: rgba(20, 12, 5, .55);
  display: flex; align-items: flex-end; justify-content: center;
  animation: fade .15s ease-out;
}
.sheet {
  position: relative; width: 100%; max-width: 520px; background: var(--card);
  border-radius: 22px 22px 0 0; padding: 1.4rem 1.1rem calc(1.4rem + var(--safe-bottom));
  animation: up .2s ease-out;
}
@media (min-width: 560px) {
  .modal { align-items: center; }
  .sheet { border-radius: 22px; }
}
.sheet h2 { font-size: 1.35rem; padding-right: 2.5rem; }
.close { position: absolute; top: .6rem; right: .6rem; font-size: 1.1rem; }
.row { display: grid; gap: .6rem; margin-top: 1.1rem; }

.scale-ends { display: flex; justify-content: space-between; color: var(--muted); font-size: .8rem; margin-bottom: .3rem; }
.scale { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; }
.scale-item {
  appearance: none; border: 2px solid transparent; background: var(--bg); color: var(--text);
  border-radius: 12px; padding: .5rem 0 .35rem; cursor: pointer; font: inherit;
  display: flex; flex-direction: column; align-items: center; gap: .15rem; min-height: 64px;
}
.scale-item span { font-size: .8rem; font-weight: 700; color: var(--muted); }
.scale-item[aria-checked=true] { border-color: var(--fg); background: var(--bg); box-shadow: 0 0 0 2px var(--fg) inset; }
.scale-item[aria-checked=true] span { color: var(--fg); }
.scale-detail {
  display: flex; align-items: center; gap: .9rem; min-height: 84px; margin: .9rem 0;
  padding: .6rem; border-radius: 14px; border: 1px dashed var(--line); justify-content: center;
}
.scale-detail:has(.detail-icon) { justify-content: flex-start; border-style: solid; }
.detail-icon { border-radius: 14px; padding: .35rem; }
#save { width: 100%; }
.field-label { display: block; font-weight: 600; font-size: .9rem; margin-bottom: .3rem; }
textarea {
  display: block; width: 100%; margin-bottom: .9rem; resize: vertical; min-height: 3.4rem; max-height: 9rem;
  font: inherit; font-size: 16px; padding: .65rem .8rem; border-radius: 12px;
  border: 1px solid var(--line); background: var(--bg); color: var(--text);
}
.place { color: var(--muted); font-size: .84rem; margin-top: .1rem; overflow-wrap: anywhere; }
.note { margin-top: .25rem; font-size: .88rem; white-space: pre-wrap; overflow-wrap: anywhere; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%); z-index: 30;
  bottom: calc(5.5rem + var(--safe-bottom)); max-width: min(92vw, 420px);
  background: #2b2118; color: #fff; padding: .7rem 1rem; border-radius: 12px; font-size: .92rem; text-align: center;
  box-shadow: 0 6px 20px rgba(0, 0, 0, .3);
}

@keyframes fade { from { opacity: 0; } }
@keyframes up { from { transform: translateY(30px); opacity: 0; } }
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
.e-btn .place { display: block; }
.sheet { max-height: 94dvh; overflow-y: auto; }
#place { display: block; width: 100%; margin-bottom: .9rem; text-align: left; }
