:root {
  --red: #EE0000;
  --red-dark: #B80000;
  --red-soft: #FFE8E8;
  --ink: #1C1C1E;
  --ink-2: #4A4A50;
  --ink-3: #8A8A92;
  --bg: #F3F3F5;
  --card: #FFFFFF;
  --line: #E3E3E8;
  --ok: #1E8E3E;
  --ok-soft: #E4F4E8;
  --warn: #B26A00;
  --warn-soft: #FFF2DC;
  --info: #1F5FBF;
  --info-soft: #E5EEFB;
  --radius: 14px;
  --tabbar-h: 64px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font-size: 16px;
  line-height: 1.4;
  min-height: 100vh;
  -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--red); }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 12px;
  padding: calc(10px + var(--safe-t)) 16px 12px;
  background: var(--red);
  color: #fff;
  box-shadow: 0 2px 10px rgba(120, 0, 0, .25);
}
.topbar-logo { width: 42px; height: 42px; border-radius: 10px; border: 2px solid rgba(255,255,255,.85); flex: none; }
.topbar-text { flex: 1; min-width: 0; }
.topbar-title { font-weight: 800; font-size: 19px; letter-spacing: -.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-sub { font-size: 12px; opacity: .85; }
.sync-pill {
  display: inline-flex; align-items: center; gap: 6px;
  background: rgba(255,255,255,.16); border: 1px solid rgba(255,255,255,.35);
  color: #fff; border-radius: 999px; padding: 6px 10px; font-size: 12px; font-weight: 600;
  cursor: pointer; flex: none;
}
.sync-pill .dot { width: 8px; height: 8px; border-radius: 50%; background: #7CFC9A; }
.sync-pill.offline .dot { background: #FFD25A; }
.sync-pill.pending .dot { background: #fff; animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { opacity: .3; } }

/* ---------- Layout ---------- */
.view {
  padding: 16px 16px calc(var(--tabbar-h) + var(--safe-b) + 96px);
  max-width: 760px; margin: 0 auto; outline: none;
}
.section-title { font-size: 13px; font-weight: 700; color: var(--ink-3); text-transform: uppercase; letter-spacing: .06em; margin: 22px 4px 8px; }
.section-title:first-child { margin-top: 4px; }

.card {
  background: var(--card); border-radius: var(--radius);
  border: 1px solid var(--line); padding: 14px;
  box-shadow: 0 1px 2px rgba(0,0,0,.03);
}
.card + .card { margin-top: 10px; }
.stack > * + * { margin-top: 12px; }
.muted { color: var(--ink-3); }
.small { font-size: 13px; }
.row { display: flex; gap: 10px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }
.center { text-align: center; }

/* ---------- Tab bar ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: grid; grid-template-columns: repeat(4, 1fr);
  height: calc(var(--tabbar-h) + var(--safe-b));
  padding-bottom: var(--safe-b);
  background: #fff; border-top: 1px solid var(--line);
}
.tabbar a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  text-decoration: none; color: var(--ink-3); font-size: 11px; font-weight: 600;
}
.tabbar svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.tabbar a.active { color: var(--red); }
.tabbar .tab-cta svg { background: var(--red); color: #fff; border-radius: 10px; padding: 3px; width: 30px; height: 30px; }
.tabbar .tab-cta.active svg { background: var(--red-dark); }

/* ---------- Forms ---------- */
label.field { display: block; }
label.field > span { display: block; font-size: 13px; font-weight: 600; color: var(--ink-2); margin: 0 2px 6px; }
input[type=text], input[type=date], input[type=search], select, textarea {
  width: 100%; border: 1px solid var(--line); background: #fff; border-radius: 10px;
  padding: 12px; font-size: 16px; /* 16px evita zoom en iOS */
  appearance: none; -webkit-appearance: none;
}
select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%238A8A92' stroke-width='2'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 10px center; background-size: 18px; padding-right: 36px;
}
textarea { min-height: 92px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--red); outline-offset: -1px; border-color: transparent; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
@media (max-width: 420px) { .grid-2 { grid-template-columns: 1fr; } }

.search { position: relative; }
.search input { padding-left: 40px; }
.search svg { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); width: 18px; height: 18px; stroke: var(--ink-3); fill: none; stroke-width: 2; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: 0; border-radius: 12px; padding: 13px 16px; font-weight: 700; font-size: 15px;
  cursor: pointer; text-decoration: none; min-height: 48px;
  background: #fff; color: var(--ink); border: 1px solid var(--line);
}
.btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.btn.primary { background: var(--red); color: #fff; border-color: var(--red); }
.btn.primary:active { background: var(--red-dark); }
.btn.dark { background: var(--ink); color: #fff; border-color: var(--ink); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--red); }
.btn.danger { color: var(--red); border-color: var(--red-soft); background: var(--red-soft); }
.btn.block { width: 100%; }
.btn:disabled { opacity: .55; cursor: default; }

/* ---------- Chips / segmented ---------- */
.chips { display: flex; gap: 8px; overflow-x: auto; padding: 2px 2px 6px; margin: 0 -2px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; border: 1px solid var(--line); background: #fff; color: var(--ink-2);
  border-radius: 999px; padding: 8px 14px; font-size: 14px; font-weight: 600; cursor: pointer;
}
.chip.active { background: var(--ink); color: #fff; border-color: var(--ink); }
.chip .count { opacity: .6; margin-left: 4px; font-weight: 500; }

.segmented { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 6px; }
.segmented button {
  border: 1px solid var(--line); background: #fff; border-radius: 10px; padding: 10px 4px;
  font-size: 13px; font-weight: 700; color: var(--ink-2); cursor: pointer; min-height: 44px;
}
.segmented button.on { color: #fff; }
.segmented button.on[data-v="OK"] { background: var(--ok); border-color: var(--ok); }
.segmented button.on[data-v="OBS"] { background: var(--warn); border-color: var(--warn); }
.segmented button.on[data-v="NA"] { background: var(--ink-3); border-color: var(--ink-3); }
.segmented.crit { grid-template-columns: repeat(5, 1fr); grid-auto-flow: row; }
@media (max-width: 420px) { .segmented.crit { grid-template-columns: repeat(3, 1fr); } }
.segmented.crit button.on { background: var(--ink); border-color: var(--ink); }
.segmented.crit button.on[data-v="Sin hallazgos"] { background: var(--ok); border-color: var(--ok); }
.segmented.crit button.on[data-v="Baja"] { background: var(--info); border-color: var(--info); }
.segmented.crit button.on[data-v="Media"] { background: var(--warn); border-color: var(--warn); }
.segmented.crit button.on[data-v="Alta"] { background: var(--red); border-color: var(--red); }
.segmented.crit button.on[data-v="Crítica"] { background: #6B0000; border-color: #6B0000; }

/* ---------- Badges ---------- */
.badge { display: inline-flex; align-items: center; gap: 5px; border-radius: 999px; padding: 3px 9px; font-size: 12px; font-weight: 700; white-space: nowrap; }
.badge.Programada { background: var(--info-soft); color: var(--info); }
.badge.En-curso { background: var(--warn-soft); color: var(--warn); }
.badge.Realizada { background: var(--ok-soft); color: var(--ok); }
.badge.Cancelada { background: #EEE; color: var(--ink-3); }
.badge.pending { background: var(--red-soft); color: var(--red-dark); }
.crit-dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; flex: none; }
.crit-Sin-hallazgos { background: var(--ok); }
.crit-Baja { background: var(--info); }
.crit-Media { background: var(--warn); }
.crit-Alta { background: var(--red); }
.crit-Crítica { background: #6B0000; }

/* ---------- Visit list ---------- */
.visit-card { display: block; text-decoration: none; color: inherit; }
.visit-card:active { background: #FAFAFB; }
.visit-card .se { font-weight: 800; font-size: 16px; }
.visit-card .meta { font-size: 13px; color: var(--ink-3); margin-top: 2px; }
.visit-card .foot { display: flex; gap: 6px; align-items: center; margin-top: 10px; flex-wrap: wrap; }
.date-box {
  flex: none; width: 52px; text-align: center; border-radius: 10px; background: var(--red-soft); color: var(--red-dark); padding: 6px 0;
}
.date-box .d { font-size: 20px; font-weight: 800; line-height: 1; }
.date-box .m { font-size: 11px; font-weight: 700; text-transform: uppercase; }
.date-box.today { background: var(--red); color: #fff; }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.stat { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; }
.stat .n { font-size: 24px; font-weight: 800; }
.stat .l { font-size: 12px; color: var(--ink-3); font-weight: 600; }
.stat.hl { background: var(--red); color: #fff; border-color: var(--red); }
.stat.hl .l { color: rgba(255,255,255,.85); }

.empty { text-align: center; padding: 40px 20px; color: var(--ink-3); }
.empty svg { width: 48px; height: 48px; stroke: var(--line); fill: none; stroke-width: 1.5; margin-bottom: 8px; }

/* ---------- Visit form ---------- */
.arrival {
  display: flex; gap: 12px; align-items: center;
  border-radius: var(--radius); padding: 14px; background: var(--ink); color: #fff;
}
.arrival.done { background: var(--ok-soft); color: var(--ink); border: 1px solid #C6E7CF; }
.arrival.far { background: var(--warn-soft); color: var(--ink); border: 1px solid #F2D7A6; }
.arrival .ic { width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; background: rgba(255,255,255,.12); flex: none; }
.arrival.done .ic { background: var(--ok); color: #fff; }
.arrival.far .ic { background: var(--warn); color: #fff; }
.arrival .ic svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; }

.chk-group h4 { margin: 0 0 8px; font-size: 14px; color: var(--red-dark); }
.chk-item { padding: 12px 0; border-top: 1px solid var(--line); }
.chk-group .chk-item:first-of-type { border-top: 0; padding-top: 4px; }
.chk-item .q { font-weight: 600; margin-bottom: 8px; font-size: 15px; }
.chk-item textarea { margin-top: 8px; min-height: 64px; }
.progress { height: 8px; background: var(--line); border-radius: 99px; overflow: hidden; }
.progress > div { height: 100%; background: var(--red); transition: width .25s; }

.photos { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 8px; }
.photo { position: relative; aspect-ratio: 1; border-radius: 10px; overflow: hidden; background: var(--line); }
.photo img { width: 100%; height: 100%; object-fit: cover; display: block; }
.photo a.link { position: absolute; inset: 0; display: grid; place-items: center; color: var(--ink-2); font-size: 12px; font-weight: 700; text-decoration: none; text-align: center; padding: 6px; }
.photo .rm { position: absolute; top: 4px; right: 4px; width: 26px; height: 26px; border-radius: 50%; border: 0; background: rgba(0,0,0,.6); color: #fff; font-size: 16px; line-height: 1; cursor: pointer; }
.photo .tag { position: absolute; left: 4px; bottom: 4px; background: rgba(0,0,0,.6); color: #fff; font-size: 10px; padding: 2px 6px; border-radius: 6px; }

.savebar {
  position: fixed; left: 0; right: 0; z-index: 25;
  bottom: calc(var(--tabbar-h) + var(--safe-b));
  padding: 10px 16px; background: rgba(243,243,245,.94);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  border-top: 1px solid var(--line);
}
.savebar .inner { max-width: 760px; margin: 0 auto; display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

/* ---------- Subestaciones ---------- */
.se-card .name { font-weight: 800; }
.se-card .actions { display: flex; gap: 8px; margin-top: 12px; }
.se-card .actions .btn { flex: 1; padding: 10px; min-height: 42px; font-size: 14px; }

/* ---------- Toast / banners ---------- */
.toast {
  position: fixed; left: 50%; transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  bottom: calc(var(--tabbar-h) + var(--safe-b) + 84px); z-index: 50;
  background: var(--ink); color: #fff; padding: 12px 16px; border-radius: 12px; font-weight: 600; font-size: 14px;
  max-width: calc(100% - 32px); box-shadow: 0 8px 24px rgba(0,0,0,.2); transition: all .25s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--red-dark); }

.install-banner {
  position: fixed; left: 12px; right: 12px; z-index: 40;
  bottom: calc(var(--tabbar-h) + var(--safe-b) + 12px);
  background: #fff; border: 1px solid var(--line); border-left: 5px solid var(--red);
  border-radius: 12px; padding: 12px 14px; box-shadow: 0 8px 24px rgba(0,0,0,.12);
  display: flex; gap: 10px; align-items: center; font-size: 14px; max-width: 736px; margin: 0 auto;
}
.install-banner .btn { min-height: 38px; padding: 8px 12px; }
.install-banner[hidden], body.in-form .install-banner { display: none; }

.notice { border-radius: 12px; padding: 12px 14px; font-size: 14px; background: var(--info-soft); color: var(--info); }
.notice.warn { background: var(--warn-soft); color: var(--warn); }
.notice.err { background: var(--red-soft); color: var(--red-dark); }

.spinner { width: 28px; height: 28px; border: 3px solid var(--line); border-top-color: var(--red); border-radius: 50%; animation: spin .8s linear infinite; margin: 40px auto; }
@keyframes spin { to { transform: rotate(360deg); } }

.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; font-size: 14px; }
.kv dt { color: var(--ink-3); }
.kv dd { margin: 0; font-weight: 600; text-align: right; word-break: break-word; }

@media (min-width: 760px) {
  .view { padding-top: 24px; }
}

/* ---------- Equipos ---------- */
.eq-item.done { background: linear-gradient(90deg, var(--ok-soft), transparent 40%); margin: 0 -14px; padding-left: 14px; padding-right: 14px; }
.eq-meta { font-size: 12px; color: var(--ink-3); margin: -4px 0 6px; }
.eq-badges { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 8px; }
.eq-label { font-size: 11px; font-weight: 700; color: var(--ink-3); text-transform: uppercase; letter-spacing: .05em; margin: 8px 2px 4px; }
.segmented.eq button, .segmented.pila button { font-size: 12.5px; padding: 8px 2px; min-height: 40px; }
.segmented.eq button.on[data-v="BUENO"], .segmented.pila button.on[data-v="OK"] { background: var(--ok); border-color: var(--ok); }
.segmented.eq button.on[data-v="MALO"], .segmented.pila button.on[data-v="PENDIENTE"] { background: var(--warn); border-color: var(--warn); }
.segmented.eq button.on[data-v="FALLADO"], .segmented.pila button.on[data-v="NOK"] { background: var(--red); border-color: var(--red); }
.segmented.eq button.on[data-v="NO ESTA"] { background: #6B0000; border-color: #6B0000; }
.pilas-sum { display: flex; gap: 10px; align-items: center; margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--line); font-size: 14px; }
.pilas-sum svg { width: 26px; height: 26px; fill: none; stroke: var(--ok); stroke-width: 2; flex: none; }
.mini-stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-top: 12px; }
.mini-stats > div { background: #F6F6F8; border-radius: 10px; padding: 8px 6px; text-align: center; }
.mini-stats b { display: block; font-size: 18px; }
.mini-stats span { font-size: 11px; color: var(--ink-3); font-weight: 600; }
.mini-stats .bad { background: var(--red-soft); } .mini-stats .bad b { color: var(--red-dark); }
.mini-stats .warn { background: var(--warn-soft); } .mini-stats .warn b { color: var(--warn); }

.eq-item.done.bad { background: linear-gradient(90deg, var(--red-soft), transparent 40%); }
