:root {
  --brand: #0f4c81;
  --brand-dark: #0a3459;
  --bg: #f3f5f8;
  --card: #ffffff;
  --text: #1b2430;
  --muted: #5f6b7a;
  --border: #dde3ea;
  --done: #1f9d55;
  --progress: #e8890c;
  --pending: #6b7a8c;
  --danger: #c0392b;
  --old: #fdecea;
  --new: #e6f6ec;
  --radius: 12px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); display: flex; flex-direction: column; min-height: 100dvh; }

.topbar {
  background: var(--brand);
  color: #fff;
  padding: 10px 16px 8px;
  padding-top: max(10px, env(safe-area-inset-top));
}
.topbar-row { display: flex; align-items: center; gap: 8px; justify-content: space-between; }
.topbar-actions { display: flex; align-items: center; gap: 8px; }
.who { color: #fff; font-size: 13px; opacity: .9; }

/* Pantalla de entrada: mientras no hay sesión se oculta todo lo demás */
/* Pantallas sin la web de la ruta (entrada, pendiente, usuarios): se oculta todo lo demás */
body.locked > *:not(.screen):not(#toast):not(script) { display: none !important; }
.login { flex: 1; display: flex; align-items: center; justify-content: center; padding: 24px 16px; }
.login-card { width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: 14px; background: #fff; border: 1px solid var(--border); border-radius: 12px; padding: 24px; }
.login-card h1 { margin: 0; font-size: 22px; color: var(--brand); }
.login-card p { margin: 0; }
.login-card select, .login-card input { font-size: 16px; padding: 10px; border: 1px solid var(--border); border-radius: 8px; background: #fff; }
.login-error { background: #fdecea; color: #8a1c14; border-radius: 8px; padding: 8px 10px; font-size: 14px; }
.topbar-row + .topbar-row { margin-top: 8px; }
.topbar h1 { font-size: 18px; margin: 0; font-weight: 700; }
#route-select {
  width: 100%;
  padding: 8px 10px;
  border-radius: 8px;
  border: 0;
  font-size: 15px;
}

.progress { margin-top: 8px; }
.progress-bar { height: 8px; background: rgba(255,255,255,.25); border-radius: 99px; overflow: hidden; }
.progress-bar span { display: block; height: 100%; background: #7ee2a8; width: 0; transition: width .3s; }
.progress-text { font-size: 13px; margin-top: 4px; opacity: .95; }

.banner {
  background: #fff7e6;
  color: #7a4b00;
  border-bottom: 1px solid #f3d9a4;
  padding: 8px 16px;
  font-size: 14px;
}

.tabs { display: flex; background: var(--card); border-bottom: 1px solid var(--border); }
.tab {
  flex: 1;
  padding: 12px;
  border: 0;
  background: none;
  font-size: 15px;
  font-weight: 600;
  color: var(--muted);
  border-bottom: 3px solid transparent;
}
.tab.active { color: var(--brand); border-bottom-color: var(--brand); }

main { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.view { display: none; flex: 1; min-height: 0; }
.view.active { display: flex; flex-direction: column; }

#view-map { position: relative; }
#map { flex: 1; min-height: 300px; }
.map-actions {
  position: absolute;
  left: 16px;
  right: 16px;
  bottom: max(16px, env(safe-area-inset-bottom));
  z-index: 500;
  display: flex;
  justify-content: center;
}

.list-tools {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 12px 16px;
  flex-wrap: wrap;
}
.list-tools input[type="search"] {
  flex: 1;
  min-width: 160px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 15px;
}
.check { display: flex; gap: 6px; align-items: center; font-size: 14px; }

#stop-list { padding: 0 16px 24px; overflow-y: auto; }
.section-title { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 16px 0 8px; }

.stop {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 10px;
  overflow: hidden;
}
.stop-head { display: flex; align-items: center; gap: 10px; padding: 10px 12px; }
.stop-num {
  flex: none;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: #fff;
  font-weight: 700;
  font-size: 13px;
  background: var(--pending);
}
.stop-num.hecha { background: var(--done); }
.stop-num.en_curso { background: var(--progress); }
.stop-addr { flex: 1; font-weight: 600; font-size: 14px; min-width: 0; overflow-wrap: anywhere; }
.stop-link { font-size: 13px; color: var(--brand); text-decoration: none; white-space: nowrap; }

.cto-row {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 10px 12px;
  border: 0;
  border-top: 1px solid var(--border);
  background: none;
  text-align: left;
  font-size: 15px;
  color: inherit;
}
.cto-row:active { background: #eef2f7; }
.cto-codes { flex: 1; min-width: 0; }
.cto-codes b { font-size: 16px; }
.cto-codes .small { display: block; overflow-wrap: anywhere; }

.badge {
  flex: none;
  font-size: 12px;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 99px;
  color: #fff;
  background: var(--pending);
}
.badge.hecha { background: var(--done); }
.badge.en_curso { background: var(--progress); }

.muted { color: var(--muted); }
.small { font-size: 13px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 14px;
  border-radius: 10px;
  border: 1px solid transparent;
  font-size: 15px;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
}
.btn:disabled { opacity: .6; cursor: default; }
.btn-primary { background: var(--brand); color: #fff; box-shadow: 0 2px 8px rgba(0,0,0,.2); }
.btn-light { background: #fff; color: var(--brand); border-color: var(--border); }
.btn-danger { background: #fff; color: var(--danger); border-color: #f0c4bf; }
.btn-sm { padding: 6px 10px; font-size: 13px; }
.icon-btn { border: 0; background: none; font-size: 20px; padding: 6px 10px; color: var(--muted); cursor: pointer; }

.empty { flex: 1; display: grid; place-items: center; align-content: center; gap: 12px; padding: 32px 16px; text-align: center; }

/* Ficha (dialog a pantalla completa en móvil) */
.sheet {
  border: 0;
  padding: 0;
  width: 100%;
  max-width: 560px;
  max-height: 100dvh;
  height: 100dvh;
  margin: 0 auto;
  border-radius: 0;
  background: var(--bg);
  color: var(--text);
}
@media (min-width: 600px) {
  .sheet { height: auto; max-height: 92dvh; margin-top: 4dvh; border-radius: var(--radius); }
}
.sheet::backdrop { background: rgba(0,0,0,.45); }
.sheet-head {
  position: sticky;
  top: 0;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
  padding: 12px 16px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  z-index: 1;
}
.sheet-body { padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.cto-title { font-size: 18px; font-weight: 700; overflow-wrap: anywhere; }

.relabel { display: grid; grid-template-columns: 1fr auto 1fr; gap: 8px; align-items: stretch; }
.relabel-box { border-radius: var(--radius); padding: 10px; text-align: center; min-width: 0; }
.relabel-box.old { background: var(--old); }
.relabel-box.new { background: var(--new); }
.relabel-label { font-size: 12px; text-transform: uppercase; font-weight: 700; color: var(--muted); }
.relabel-code { font-size: 26px; font-weight: 800; margin: 2px 0; }
.relabel-name { font-size: 12px; color: var(--muted); overflow-wrap: anywhere; }
.relabel-arrow { align-self: center; font-size: 24px; font-weight: 700; color: var(--muted); }

.address { font-weight: 600; }
.row-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.row-actions .btn { flex: 1; min-width: 140px; }

.photos-head { display: flex; align-items: center; justify-content: space-between; }
.photos-title { margin: 0; font-size: 16px; }
.photo-slots { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.photo-slot { min-width: 0; display: flex; flex-direction: column; gap: 4px; }
.photo-slot-label { font-size: 13px; font-weight: 700; text-align: center; }
.photo-frame {
  position: relative;
  aspect-ratio: 1 / 1;
  border-radius: 10px;
  background: #e9edf2;
  border: 2px dashed #b9c3cf;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  overflow: hidden;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  text-align: center;
  padding: 0;
  width: 100%;
  cursor: pointer;
}
.photo-frame.has-photo { border: 2px solid var(--done); background: #000; }
.photo-frame .cam { font-size: 26px; line-height: 1; }
.photo-frame img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.photo-frame input[type="file"] { display: none; }
.photo-frame .uploading {
  position: absolute;
  inset: 0;
  background: rgba(255,255,255,.85);
  display: grid;
  place-items: center;
  font-weight: 700;
  color: var(--brand);
}
.photo-count {
  position: absolute;
  right: 4px;
  bottom: 4px;
  background: rgba(0,0,0,.7);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: 99px;
}
.btn-add { padding: 6px 4px; font-size: 12px; border-radius: 8px; }
.btn-add input[type="file"] { display: none; }
.photo-full { font-size: 12px; color: var(--muted); text-align: center; padding: 6px 0; }

/* Visor de fotos a pantalla completa */
.viewer {
  border: 0;
  padding: 0;
  width: 100vw;
  height: 100dvh;
  max-width: none;
  max-height: none;
  margin: 0;
  background: #000;
  color: #fff;
}
.viewer::backdrop { background: #000; }
.viewer[open] { display: flex; flex-direction: column; }
.viewer-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 16px;
  padding-top: max(10px, env(safe-area-inset-top));
  font-weight: 700;
}
.icon-btn.light { color: #fff; }
.viewer-body { position: relative; flex: 1; display: grid; place-items: center; min-height: 0; }
.viewer-body img { max-width: 100%; max-height: 100%; object-fit: contain; }
.viewer-nav {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  border: 0;
  background: rgba(255,255,255,.18);
  color: #fff;
  font-size: 34px;
  width: 44px;
  height: 64px;
  border-radius: 10px;
  cursor: pointer;
}
.viewer-nav.prev { left: 8px; }
.viewer-nav.next { right: 8px; }
.viewer-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  padding: 10px 16px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  font-size: 13px;
}
.viewer-meta span { opacity: .85; }

.field { display: flex; flex-direction: column; gap: 6px; font-size: 14px; font-weight: 600; }
.field input { font-size: 15px; padding: 8px; border: 1px solid var(--border); border-radius: 8px; background: #fff; }

.toast {
  position: fixed;
  left: 16px;
  right: 16px;
  bottom: max(20px, env(safe-area-inset-bottom));
  margin: 0 auto;
  max-width: 480px;
  background: #1b2430;
  color: #fff;
  padding: 12px 14px;
  border-radius: 10px;
  font-size: 14px;
  z-index: 2000;
  box-shadow: 0 4px 16px rgba(0,0,0,.3);
}
.toast.error { background: var(--danger); }

/* Marcadores del mapa */
.marker {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: #fff;
  font: 700 12px system-ui, sans-serif;
  border: 2px solid #fff;
  box-shadow: 0 1px 4px rgba(0,0,0,.4);
  background: var(--pending);
}
.marker.hecha { background: var(--done); }
.marker.en_curso { background: var(--progress); }
.me-marker {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #2b7cff;
  border: 3px solid #fff;
  box-shadow: 0 0 0 4px rgba(43,124,255,.3);
}
.popup-ctos { margin: 6px 0 0; padding: 0; list-style: none; }
.popup-ctos li { margin: 4px 0; }
.popup-ctos button {
  border: 1px solid var(--border);
  background: #fff;
  border-radius: 8px;
  padding: 6px 8px;
  width: 100%;
  text-align: left;
  font-size: 14px;
  cursor: pointer;
}

/* Entrar / Crear cuenta */
.login-tabs { display: flex; gap: 4px; background: var(--bg); border-radius: 10px; padding: 4px; }
.login-tab { flex: 1; border: 0; background: transparent; padding: 8px; border-radius: 8px; font: inherit; font-weight: 600; color: var(--muted, #555); cursor: pointer; }
.login-tab.active { background: #fff; color: var(--brand); box-shadow: 0 1px 2px rgba(0,0,0,.08); }
.login-form { display: flex; flex-direction: column; gap: 14px; }
.field .hint { font-weight: 400; font-size: 12px; color: var(--muted, #666); }

/* Usuarios (admin) */
.admin { flex: 1; display: flex; flex-direction: column; }
.admin-body { padding: 16px; max-width: 720px; width: 100%; margin: 0 auto; display: flex; flex-direction: column; gap: 12px; }
.admin-body p { margin: 0; }
.user-card { background: #fff; border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; display: grid; gap: 10px; }
.user-card.pending { border-color: #e0a800; box-shadow: inset 4px 0 0 #e0a800; }
.user-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.user-name { font-weight: 700; }
.user-actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.user-actions select { font-size: 15px; padding: 7px 8px; border: 1px solid var(--border); border-radius: 8px; background: #fff; flex: 1 1 160px; }
.tag-pending { background: #fff3cd; color: #7a5a00; border-radius: 999px; padding: 2px 8px; font-size: 12px; font-weight: 600; }

/* Botón «Usuarios» (admin) con el número de cuentas pendientes */
.count-badge { display: inline-block; min-width: 18px; padding: 0 5px; margin-left: 2px; border-radius: 999px; background: #e0a800; color: #fff; font-size: 12px; line-height: 18px; text-align: center; }
.topbar-actions { flex-wrap: wrap; justify-content: flex-end; }

/* Cabecera compacta: título en una línea + menú ☰ con las acciones poco usadas */
.topbar h1 { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.menu-wrap { position: relative; flex: none; }
.menu-btn { position: relative; display: inline-flex; align-items: center; justify-content: center; width: 40px; height: 40px; border: 1px solid rgba(255,255,255,.35); border-radius: 10px; background: rgba(255,255,255,.12); color: #fff; font-size: 20px; cursor: pointer; }
.menu-btn:hover, .menu-btn[aria-expanded="true"] { background: rgba(255,255,255,.22); }
.menu-btn .count-badge { position: absolute; top: -6px; right: -6px; margin: 0; box-shadow: 0 0 0 2px var(--brand); }
.app-menu { position: absolute; right: 0; top: calc(100% + 6px); z-index: 1200; min-width: 220px; background: #fff; color: var(--text, #1d2733); border: 1px solid var(--border); border-radius: 12px; box-shadow: 0 8px 24px rgba(0,0,0,.18); padding: 6px; display: flex; flex-direction: column; }
.menu-who { padding: 8px 10px 10px; border-bottom: 1px solid var(--border); margin-bottom: 4px; }
.menu-who-name { font-weight: 700; }
.menu-who-role { font-size: 13px; color: var(--muted); }
.menu-item { display: flex; align-items: center; justify-content: space-between; gap: 8px; width: 100%; text-align: left; border: 0; background: none; font: inherit; font-size: 15px; padding: 11px 10px; border-radius: 8px; color: inherit; cursor: pointer; }
.menu-item:hover, .menu-item:focus-visible { background: var(--bg); outline: none; }
.menu-item[data-logout] { color: #b3261e; }
.topbar select#route-select { min-width: 0; width: 100%; }

/* Avance: barra gruesa con la cifra al lado */
.progress-line { display: flex; align-items: center; gap: 10px; }
.progress-line .progress-bar { flex: 1; height: 12px; }
.progress-count { font-size: 14px; white-space: nowrap; }
.progress-count b { font-size: 18px; }
.progress-text { font-size: 13px; margin-top: 2px; opacity: .85; }

/* En pantallas anchas no hace falta el menú ☰: las acciones van a la vista en la cabecera */
@media (min-width: 768px) {
  .menu-btn { display: none; }
  .app-menu, .app-menu[hidden] {
    display: flex !important; position: static; flex-direction: row; align-items: center; gap: 8px;
    min-width: 0; padding: 0; background: none; border: 0; box-shadow: none; color: #fff;
  }
  .app-menu .menu-who { display: flex; flex-direction: column; align-items: flex-end; border: 0; margin: 0 4px 0 0; padding: 0; order: 2; }
  .app-menu .menu-who-name { font-weight: 600; font-size: 14px; }
  .app-menu .menu-who-role { color: #fff; opacity: .75; font-size: 12px; }
  .app-menu .menu-item {
    width: auto; padding: 6px 12px; font-size: 14px; font-weight: 600; border-radius: 8px;
    background: #fff; color: var(--brand); border: 1px solid var(--border);
  }
  .app-menu .menu-item[hidden] { display: none; }
  .app-menu .menu-item:hover, .app-menu .menu-item:focus-visible { background: #eef3f8; }
  .app-menu #btn-import { order: 1; }
  .app-menu #btn-users { order: 1; }
  .app-menu .menu-item[data-logout] { order: 3; color: var(--brand); }
}

/* Alta de usuarios (admin) */
.new-user > summary { list-style: none; display: inline-flex; width: auto; cursor: pointer; }
.new-user > summary::-webkit-details-marker { display: none; }
.new-user[open] > summary { margin-bottom: 12px; }
.new-user-form { background: #fff; border: 1px solid var(--border); border-radius: 12px; padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.new-user-form select, .new-user-form input { font-size: 16px; padding: 9px; border: 1px solid var(--border); border-radius: 8px; background: #fff; }
