:root {
  --navy: #0b1f3a;
  --navy-2: #143061;
  --blue: #1f5fbf;
  --blue-soft: #e6eefb;
  --bg: #f1f4f8;
  --card: #ffffff;
  --line: #d9e0ea;
  --text: #13213a;
  --muted: #5d6b82;
  --ok: #157347;
  --ok-soft: #e3f4ea;
  --warn: #8a5a00;
  --warn-soft: #fff4d6;
  --err: #b42318;
  --err-soft: #fdecea;
  --radius: 14px;
  --tap: 52px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  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-size: 16px;
  line-height: 1.45;
  padding-bottom: env(safe-area-inset-bottom);
}

/* ---------- Encabezado ---------- */
.topbar {
  background: var(--navy);
  color: #fff;
  padding: calc(14px + env(safe-area-inset-top)) 16px 14px;
  position: sticky;
  top: 0;
  z-index: 20;
  box-shadow: 0 2px 10px rgba(0, 0, 0, .18);
}
.topbar-inner {
  max-width: 960px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  gap: 12px;
}
.brand { white-space: nowrap; display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 1.2rem; color: #fff; text-decoration: none; }
.brand img { width: 34px; height: 34px; border-radius: 9px; }
.topbar .spacer { flex: 1; }
.topbar .btn { white-space: nowrap; }
@media (max-width: 460px) { .topbar .brand-text { display: none; } .topbar-inner { gap: 8px; } }
@media (max-width: 330px) { .topbar .badge { display: none; } }
.badge {
  font-size: .78rem;
  padding: 4px 10px;
  border-radius: 999px;
  background: rgba(255, 255, 255, .14);
  white-space: nowrap;
}
.badge.offline { background: #8a5a00; }

/* ---------- Navegación por secciones ---------- */
.tabs {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  max-width: 960px;
  margin: 0 auto;
  padding: 10px 16px 0;
  scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tabs a {
  flex: 1 0 auto;
  text-align: center;
  padding: 10px 14px;
  border-radius: 999px;
  text-decoration: none;
  color: var(--navy);
  background: #fff;
  border: 1px solid var(--line);
  font-weight: 600;
  font-size: .92rem;
  white-space: nowrap;
}
.tabs a span { color: var(--muted); font-weight: 500; }

main { max-width: 960px; margin: 0 auto; padding: 12px 16px 40px; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px 16px;
  margin: 14px 0;
  box-shadow: 0 1px 2px rgba(11, 31, 58, .05);
  scroll-margin-top: 90px;
}
.card h2 {
  margin: 0 0 4px;
  font-size: 1.2rem;
  color: var(--navy);
  display: flex;
  align-items: center;
  gap: 10px;
}
.step {
  display: inline-grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--navy);
  color: #fff;
  font-size: .9rem;
  flex: none;
}
.hint { color: var(--muted); font-size: .92rem; margin: 2px 0 12px; }
.count { margin-left: auto; font-size: .9rem; color: var(--muted); font-weight: 500; }
.count b { color: var(--navy); font-size: 1.05rem; }

/* ---------- Formularios ---------- */
label { font-weight: 600; font-size: .92rem; display: block; margin-bottom: 6px; }
input[type=text], input[type=password], input[type=search], textarea, select {
  width: 100%;
  font: inherit;
  font-size: 16px; /* evita el zoom automático de iPhone */
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  color: var(--text);
  min-height: var(--tap);
}
textarea { min-height: 110px; resize: vertical; }
input:focus, textarea:focus, select:focus, button:focus-visible, a:focus-visible {
  outline: 3px solid rgba(31, 95, 191, .35);
  outline-offset: 1px;
  border-color: var(--blue);
}
.row { display: flex; gap: 8px; align-items: stretch; }
.row > input { flex: 1; min-width: 0; }
.stack > * + * { margin-top: 10px; }

details.more { margin-top: 10px; }
details.more summary {
  cursor: pointer;
  color: var(--blue);
  font-weight: 600;
  padding: 8px 0;
  list-style: none;
}
details.more summary::-webkit-details-marker { display: none; }
details.more summary::before { content: "+ "; }
details.more[open] summary::before { content: "− "; }

/* ---------- Botones ---------- */
.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--navy);
  font: inherit;
  font-weight: 600;
  min-height: var(--tap);
  padding: 10px 18px;
  border-radius: 12px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  text-decoration: none;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition: background .15s, transform .05s;
}
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: var(--navy); border-color: var(--navy); color: #fff; }
.btn.primary:hover:not(:disabled) { background: var(--navy-2); }
.btn.blue { background: var(--blue); border-color: var(--blue); color: #fff; }
.btn.danger { color: var(--err); border-color: #f1c4bf; }
.btn.ghost { background: transparent; border-color: transparent; color: var(--blue); }
.btn.big { min-height: 64px; font-size: 1.15rem; width: 100%; border-radius: 16px; }
.btn.small { min-height: 40px; padding: 6px 12px; font-size: .9rem; }
.btn-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 8px; margin-top: 12px; }
.icon-btn {
  appearance: none;
  border: 0;
  background: transparent;
  width: 44px;
  height: 44px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  cursor: pointer;
  color: var(--muted);
  font-size: 1.1rem;
  flex: none;
}
.icon-btn:hover { background: var(--bg); color: var(--navy); }
.icon-btn.del:hover { color: var(--err); background: var(--err-soft); }
.file-btn { position: relative; overflow: hidden; }
.file-btn input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }

/* ---------- Listas ---------- */
.list {
  list-style: none;
  margin: 12px 0 0;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 12px;
  max-height: 420px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.list:empty::after {
  content: attr(data-empty);
  display: block;
  padding: 18px;
  text-align: center;
  color: var(--muted);
}
.list li {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 4px 4px 12px;
  border-bottom: 1px solid var(--line);
  min-height: 52px;
}
.list li:last-child { border-bottom: 0; }
.list li .idx { color: var(--muted); font-size: .85rem; width: 2.2em; flex: none; font-variant-numeric: tabular-nums; }
.list li .txt { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.list li.dup { background: var(--warn-soft); }
.list li.dup .txt::after { content: "repetido"; margin-left: 8px; font-size: .75rem; color: var(--warn); font-weight: 700; text-transform: uppercase; }
.list li input { flex: 1; min-height: 44px; padding: 8px 10px; }

/* ---------- Mensajes ---------- */
.alert {
  border-radius: 12px;
  padding: 12px 14px;
  margin: 12px 0 0;
  font-size: .95rem;
  display: flex;
  gap: 10px;
  align-items: flex-start;
  flex-wrap: wrap;
}
.alert[hidden] { display: none; }
.alert.warn { background: var(--warn-soft); color: var(--warn); }
.alert.err { background: var(--err-soft); color: var(--err); }
.alert.ok { background: var(--ok-soft); color: var(--ok); }
.alert.info { background: var(--blue-soft); color: var(--navy); }
.alert p { margin: 0; flex: 1 1 220px; }
.alert .btn { min-height: 40px; }

/* ---------- Sorteo ---------- */
.mode {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin: 8px 0 14px;
}
.mode label {
  margin: 0;
  border: 2px solid var(--line);
  border-radius: 12px;
  padding: 12px;
  cursor: pointer;
  font-weight: 500;
  font-size: .9rem;
  color: var(--muted);
}
.mode label b { display: block; color: var(--navy); font-size: 1rem; margin-bottom: 2px; }
.mode input { position: absolute; opacity: 0; pointer-events: none; }
.mode label:has(input:checked) { border-color: var(--navy); background: var(--blue-soft); }
.summary-nums {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 12px;
}
.summary-nums div {
  background: var(--bg);
  border-radius: 12px;
  padding: 12px;
  text-align: center;
}
.summary-nums b { display: block; font-size: 1.8rem; color: var(--navy); line-height: 1.1; }
.summary-nums span { color: var(--muted); font-size: .85rem; }

.spinner-box {
  margin-top: 14px;
  background: var(--navy);
  color: #fff;
  border-radius: 16px;
  padding: 26px 16px;
  text-align: center;
}
.spinner-box[hidden] { display: none; }
.spinner-box .rolling {
  font-size: 1.5rem;
  font-weight: 700;
  min-height: 2.2em;
  display: grid;
  place-items: center;
  overflow-wrap: anywhere;
}
.spinner-box .bar { height: 6px; background: rgba(255, 255, 255, .2); border-radius: 3px; overflow: hidden; margin-top: 10px; }
.spinner-box .bar i { display: block; height: 100%; width: 0; background: #fff; border-radius: 3px; }

/* ---------- Tabla de resultados ---------- */
.table-wrap { overflow-x: auto; margin-top: 12px; border: 1px solid var(--line); border-radius: 12px; }
table.results { width: 100%; border-collapse: collapse; font-size: .97rem; }
table.results th {
  background: var(--navy);
  color: #fff;
  text-align: left;
  padding: 10px 12px;
  font-weight: 600;
  position: sticky;
  top: 0;
}
table.results td { padding: 10px 12px; border-top: 1px solid var(--line); overflow-wrap: anywhere; }
table.results tr:nth-child(even) td { background: #f8fafc; }
table.results td.num { color: var(--muted); width: 3.5em; font-variant-numeric: tabular-nums; }
table.results td.asg { font-weight: 700; color: var(--navy); }
table.results tr.hit td { background: #fff4d6 !important; }
tr.reveal { animation: reveal .35s both; }
@keyframes reveal { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

.result-head { display: flex; flex-wrap: wrap; align-items: baseline; gap: 6px 12px; }
.result-head h3 { margin: 0; font-size: 1.15rem; color: var(--navy); }
.result-head .meta { color: var(--muted); font-size: .9rem; }
.pill { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: .78rem; font-weight: 700; }
.pill.draft { background: var(--warn-soft); color: var(--warn); }
.pill.pub { background: var(--ok-soft); color: var(--ok); }

/* ---------- Historial ---------- */
.history { list-style: none; margin: 10px 0 0; padding: 0; }
.history li {
  display: flex;
  align-items: center;
  gap: 8px;
  border: 1px solid var(--line);
  border-radius: 12px;
  margin-bottom: 8px;
  background: #fff;
}
.history li.active { border-color: var(--navy); box-shadow: inset 4px 0 0 var(--navy); }
.history button.open {
  flex: 1;
  text-align: left;
  border: 0;
  background: transparent;
  padding: 12px 14px;
  font: inherit;
  cursor: pointer;
  min-height: var(--tap);
  color: var(--text);
}
.history button.open b { display: block; color: var(--navy); }
.history button.open span { font-size: .85rem; color: var(--muted); }

/* ---------- Barra de sesión ---------- */
.session-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 4px 10px;
  font-size: .92rem;
  color: var(--muted);
  padding: 6px 2px 0;
}
.session-bar b { color: var(--navy); }
.session-bar .btn { min-height: 40px; padding: 4px 6px; }

/* ---------- Login ---------- */
.login { max-width: 420px; margin: 8vh auto 0; }
.login .card { padding: 26px 20px; }

/* ---------- Avisos flotantes ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + env(safe-area-inset-bottom));
  transform: translate(-50%, 140%);
  background: var(--navy);
  color: #fff;
  padding: 12px 18px;
  border-radius: 12px;
  font-weight: 600;
  max-width: calc(100% - 32px);
  box-shadow: 0 6px 20px rgba(0, 0, 0, .25);
  transition: transform .25s;
  z-index: 50;
  text-align: center;
  pointer-events: none;
}
.toast.show { transform: translate(-50%, 0); }
.toast.err { background: var(--err); }
.toast.ok { background: var(--ok); }

footer { text-align: center; color: var(--muted); font-size: .85rem; padding: 10px 16px 30px; }
footer a { color: var(--muted); }
.empty-state { text-align: center; padding: 30px 10px; color: var(--muted); }
.empty-state b { display: block; color: var(--navy); font-size: 1.1rem; margin-bottom: 6px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

@media (min-width: 720px) {
  .two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
  .two-col > .card { margin: 14px 0 0; }
}

/* ---------- Impresión ---------- */
.print-only { display: none; }
@media print {
  body { background: #fff; font-size: 12pt; }
  .topbar, .tabs, footer, .no-print, .toast, .btn, .btn-grid, .alert, .history, .search-box { display: none !important; }
  main { padding: 0; max-width: none; }
  .card { border: 0; box-shadow: none; padding: 0; margin: 0; }
  .card:not(.printable) { display: none !important; }
  .print-only { display: block; }
  .table-wrap { border: 0; overflow: visible; }
  table.results th { background: #eee !important; color: #000; position: static; }
  table.results td, table.results th { border: 1px solid #999; padding: 6px 8px; }
  table.results tr { break-inside: avoid; }
  tr.reveal { animation: none; }
}
