:root {
  --bg: #0b1622;
  --panel: #13243a;
  --panel2: #1c3350;
  --text: #ffffff;
  --muted: #b9c7d8;
  --accent: #ffd500;
  --accent-text: #111;
  --danger: #ff3b30;
  --ok: #2ee66b;
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
  --bar-h: 60px;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--text);
  font: 18px/1.35 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%; overscroll-behavior: none;
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
}
textarea, input { -webkit-user-select: text; user-select: text; }
button { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* Barra superior */
#topbar {
  position: fixed; z-index: 20; top: 0; left: 0; right: 0;
  padding: calc(var(--sat) + 6px) calc(var(--sar) + 8px) 6px calc(var(--sal) + 8px);
  display: flex; gap: 8px; align-items: center;
  background: rgba(11, 22, 34, .94); border-bottom: 2px solid #000;
}
.tbtn {
  min-height: 48px; min-width: 56px; padding: 0 14px; border-radius: 12px;
  background: var(--panel2); border: 2px solid #3a5a80; font-weight: 700; font-size: 17px;
  position: relative;
}
.tbtn:active, .btn:active, .fab:active { transform: scale(.96); filter: brightness(1.2); }
.dot { position: absolute; top: 6px; right: 6px; width: 12px; height: 12px; border-radius: 50%; background: var(--accent); }
.progreso { flex: 1; text-align: center; font-weight: 800; font-size: 20px; white-space: nowrap; }

/* Mapa */
#mapView { position: fixed; inset: 0; }
#viewport {
  position: absolute; inset: 0; overflow: hidden; background: #fff;
  touch-action: none; cursor: grab;
}
#stage { position: absolute; left: 0; top: 0; transform-origin: 0 0; will-change: transform; }
#plano { display: block; pointer-events: none; -webkit-user-drag: none; }
#overlay { position: absolute; left: 0; top: 0; overflow: visible; pointer-events: none; }
#overlay .ped { stroke-width: 3; vector-effect: non-scaling-stroke; }
#overlay .ped.vacio { fill-opacity: .12; }
#overlay .ped.lleno { fill-opacity: .55; }
#overlay .ped.sel { stroke: #000 !important; stroke-width: 5; }
#overlay .badge circle { fill: #000; stroke: #fff; vector-effect: non-scaling-stroke; stroke-width: 2; }
#overlay .badge text { fill: #fff; font-weight: 800; text-anchor: middle; dominant-baseline: central; font-family: -apple-system, sans-serif; }
#overlay .oculto { display: none; }

.mapbtns {
  position: absolute; right: calc(var(--sar) + 12px); bottom: calc(var(--sab) + 16px);
  display: flex; flex-direction: column; gap: 10px; z-index: 5;
}
.fab {
  width: 60px; height: 60px; border-radius: 50%; border: 3px solid #fff;
  background: #0E2A47; font-size: 32px; font-weight: 700; line-height: 1;
  box-shadow: 0 2px 10px rgba(0,0,0,.5);
}
.fab.small { font-size: 26px; }

/* Lista */
#listView {
  position: fixed; inset: 0; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: calc(var(--sat) + var(--bar-h) + 12px) calc(var(--sar) + 12px) calc(var(--sab) + 24px) calc(var(--sal) + 12px);
}
.list-head { margin-bottom: 12px; }
.big-prog { font-size: 26px; font-weight: 800; margin-bottom: 8px; }
.bar { height: 16px; background: #2a3e56; border-radius: 8px; overflow: hidden; }
.bar > div { height: 100%; background: var(--ok); width: 0; }
.grupo { margin: 18px 0 6px; display: flex; align-items: center; gap: 10px; font-weight: 800; font-size: 20px; }
.grupo .cnt { margin-left: auto; font-size: 16px; color: var(--muted); }
.swatch { width: 26px; height: 26px; border-radius: 6px; border: 2px solid #fff; flex: none; display: inline-block; }
.fila {
  display: flex; align-items: center; gap: 12px; width: 100%; min-height: 58px; padding: 8px 14px;
  margin: 6px 0; background: var(--panel); border: 2px solid #2b4566; border-radius: 12px; text-align: left; font-size: 19px;
}
.fila .pid { font-weight: 700; flex: 1; }
.fila .nf { min-width: 44px; text-align: center; padding: 4px 10px; border-radius: 20px; background: #2a3e56; font-weight: 800; }
.fila .nf.si { background: var(--ok); color: #000; }
.fila .nt { font-size: 15px; color: var(--muted); }

/* Paneles */
.panel {
  position: fixed; z-index: 30; inset: 0; background: rgba(0,0,0,.55);
  display: flex; justify-content: center; align-items: flex-start;
  padding: calc(var(--sat) + 8px) calc(var(--sar) + 8px) calc(var(--sab) + 8px) calc(var(--sal) + 8px);
}
.panel-inner {
  width: 100%; max-width: 640px; max-height: 100%; overflow-y: auto; background: var(--panel);
  border: 2px solid #3a5a80; border-radius: 16px; padding: 16px;
}
.panel-title { display: flex; justify-content: space-between; align-items: center; font-size: 22px; font-weight: 800; margin-bottom: 12px; }
.close { background: var(--panel2); border: 2px solid #3a5a80; border-radius: 12px; min-width: 48px; min-height: 48px; font-size: 22px; }
.close.big { min-width: 56px; min-height: 56px; margin-left: auto; }
.seg { display: flex; border: 2px solid #3a5a80; border-radius: 12px; overflow: hidden; margin-bottom: 14px; }
.seg button { flex: 1; min-height: 52px; background: transparent; border: 0; font-weight: 700; }
.seg button.on { background: var(--accent); color: var(--accent-text); }
.chips-actions { display: flex; gap: 8px; margin-bottom: 10px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  display: flex; align-items: center; gap: 8px; min-height: 50px; padding: 6px 14px 6px 8px;
  border-radius: 25px; border: 3px solid #fff; background: var(--panel2); font-weight: 800; font-size: 17px;
}
.chip.off { opacity: .35; border-style: dashed; }
.chip .swatch { width: 30px; height: 30px; border-radius: 50%; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 52px; padding: 0 18px; border-radius: 12px; background: var(--panel2);
  border: 2px solid #4d6f96; font-weight: 700; font-size: 18px; cursor: pointer; text-align: center;
}
.btn.small { min-height: 44px; font-size: 16px; }
.btn.block { display: flex; width: 100%; margin-bottom: 10px; }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: #fff; }
.btn.danger { background: var(--danger); border-color: #fff; color: #fff; }
.btn.huge { min-height: 68px; font-size: 20px; flex: 1; white-space: nowrap; padding: 0 10px; }
.info { color: var(--muted); margin: 10px 0; }
.info.small { font-size: 15px; }
.ver-link { all: unset; color: inherit; cursor: pointer; text-decoration: underline dotted; text-underline-offset: 3px; -webkit-tap-highlight-color: transparent; }

/* Bottom sheet */
.backdrop { position: fixed; inset: 0; z-index: 39; background: rgba(0,0,0,.4); }
.sheet {
  position: fixed; z-index: 40; left: 0; right: 0; bottom: 0; margin: 0 auto; max-width: 720px;
  max-height: 82vh; max-height: 82dvh; display: flex; flex-direction: column;
  background: var(--panel); border: 2px solid #3a5a80; border-bottom: 0; border-radius: 20px 20px 0 0;
  transform: translateY(105%); transition: transform .25s cubic-bezier(.2,.8,.2,1);
  padding-bottom: var(--sab);
}
.sheet.open { transform: translateY(0); }
.grip { width: 56px; height: 6px; border-radius: 3px; background: #5b7aa0; margin: 8px auto 4px; flex: none; }
.sheet-head { display: flex; align-items: center; gap: 12px; padding: 4px calc(var(--sar) + 16px) 10px calc(var(--sal) + 16px); flex: none; touch-action: none; }
.sheet-head .swatch { width: 36px; height: 36px; }
.sheet-id { font-size: 26px; font-weight: 800; display: flex; align-items: center; flex-wrap: wrap; gap: 4px 10px; }
.num-badge { font-size: 15px; font-weight: 700; padding: 3px 10px; border-radius: 999px; background: rgba(255,255,255,.12); border: 1px solid rgba(255,255,255,.22); white-space: nowrap; }
.num-badge small { font-weight: 500; opacity: .7; font-size: 13px; }
.fila .pid .num { font-variant-numeric: tabular-nums; }
.sheet-tipo { color: var(--muted); font-size: 17px; }
.sheet-body { overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 0 calc(var(--sar) + 16px) 16px calc(var(--sal) + 16px); }
.photo-actions { display: flex; gap: 10px; }
.thumbs { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 8px; margin: 14px 0; }
.thumb { position: relative; aspect-ratio: 1; border-radius: 10px; overflow: hidden; border: 2px solid #4d6f96; background: #22364f; padding: 0; }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb .fecha { position: absolute; left: 0; right: 0; bottom: 0; font-size: 12px; background: rgba(0,0,0,.7); padding: 2px 4px; text-align: center; }
.thumbs .vacio { grid-column: 1 / -1; color: var(--muted); text-align: center; padding: 14px; border: 2px dashed #3a5a80; border-radius: 10px; }
.lbl { display: block; font-weight: 700; margin-top: 6px; margin-bottom: 6px; }
textarea {
  width: 100%; font: inherit; font-size: 18px; /* >=16px evita zoom automático en iOS */
  background: #0b1622; color: #fff; border: 2px solid #4d6f96; border-radius: 12px; padding: 10px; resize: vertical;
}

/* Visor */
/* visor: barras arriba/abajo fuera de la foto (no la tapan); la foto ocupa el espacio del medio */
.viewer { position: fixed; inset: 0; z-index: 60; background: #000; display: flex; flex-direction: column; }
.viewer-area { position: relative; flex: 1; min-height: 0; overflow: hidden; touch-action: none; cursor: zoom-in; }
.viewer-area.zoom { cursor: grab; }
.viewer-area img { position: absolute; inset: 0; margin: auto; max-width: 100%; max-height: 100%; user-select: none; -webkit-user-drag: none; transform-origin: center center; will-change: transform; }
.viewer-area img.anim { transition: transform .2s ease-out; }
.viewer-bar { position: relative; flex: none; display: flex; gap: 10px; align-items: center; justify-content: space-between; padding: 10px calc(var(--sar) + 12px) 10px calc(var(--sal) + 12px); background: rgba(0,0,0,.7); }
.viewer-bar.top { padding-top: calc(var(--sat) + 10px); }
.viewer-bar.bottom { padding-bottom: calc(var(--sab) + 12px); }
#viewerInfo { font-weight: 700; }

/* Diálogo */
.dialog { position: fixed; inset: 0; z-index: 80; background: rgba(0,0,0,.65); display: flex; align-items: center; justify-content: center; padding: 20px; }
.dialog-box { width: 100%; max-width: 480px; background: var(--panel); border: 3px solid #fff; border-radius: 16px; padding: 20px; font-size: 19px; max-height: 90vh; overflow-y: auto; }
.dialog-box ol { padding-left: 22px; }
.dialog-box li { margin-bottom: 8px; }
.dialog-btns { display: flex; flex-direction: column; gap: 10px; margin-top: 18px; }

.toast {
  position: fixed; z-index: 90; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--sab) + 24px); background: #fff; color: #000; font-weight: 700;
  padding: 14px 20px; border-radius: 14px; box-shadow: 0 4px 20px rgba(0,0,0,.5); max-width: 90vw; text-align: center;
}

/* Modos Ver / Editar */
body.modo-ver .solo-editar, body.modo-editar .solo-ver { display: none !important; }
.tbtn.modo { min-width: 92px; letter-spacing: .5px; }
body.modo-ver .tbtn.modo { background: #1f6fd1; border-color: #fff; color: #fff; }
body.modo-editar .tbtn.modo { background: var(--accent); border-color: #fff; color: var(--accent-text); }
body.modo-editar #topbar { border-bottom: 4px solid var(--accent); }
body.modo-ver .thumbs { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); }
.nota-ver { white-space: pre-wrap; background: #0b1622; border: 2px solid #2b4566; border-radius: 12px; padding: 10px; min-height: 48px; -webkit-user-select: text; user-select: text; }
.nota-ver.vacia { color: var(--muted); font-style: italic; }
.tbtn { flex: none; white-space: nowrap; }
.progreso { min-width: 0; overflow: hidden; text-overflow: ellipsis; }
@media (max-width: 560px) { .progreso .extra { display: none; } .tbtn { padding: 0 10px; min-width: 48px; } .tbtn.modo { min-width: 82px; } #topbar { gap: 6px; } }

/* Planos y elementos manuales */
.plano-chip {
  position: absolute; z-index: 6; left: calc(var(--sal) + 10px); top: calc(var(--sat) + var(--bar-h) + 10px);
  min-height: 48px; padding: 0 16px; border-radius: 24px; border: 3px solid #fff;
  background: #0E2A47; font-weight: 800; font-size: 17px; max-width: 70vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  box-shadow: 0 2px 10px rgba(0,0,0,.5);
}
.agregar { position: absolute; z-index: 6; left: calc(var(--sal) + 12px); bottom: calc(var(--sab) + 16px); min-height: 60px; font-size: 19px; box-shadow: 0 2px 10px rgba(0,0,0,.5); }
.banner {
  position: absolute; z-index: 7; left: 10px; right: 10px; top: calc(var(--sat) + var(--bar-h) + 66px);
  display: flex; gap: 10px; align-items: center; justify-content: space-between;
  background: var(--accent); color: #000; font-weight: 800; padding: 10px 12px; border-radius: 14px; border: 3px solid #000;
}
.banner .btn { background: #000; color: #fff; border-color: #000; flex: none; }
#overlay .ped.manual { stroke-dasharray: 6 4; }
.manual-actions { margin-top: 16px; padding-top: 12px; border-top: 2px solid #2b4566; display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.manual-actions .info { width: 100%; margin: 0; }
.manual-actions .btn { flex: 1; }
.plano-item { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.plano-item .btn { flex: 1; justify-content: space-between; min-height: 60px; }
.plano-item .btn.on { border-color: var(--accent); border-width: 3px; }
.plano-item .mini { flex: none; min-width: 56px; }
.form label { display: block; font-weight: 700; margin: 12px 0 6px; }
.form input[type=text], .form select {
  width: 100%; font: inherit; font-size: 18px; min-height: 52px; padding: 0 12px;
  background: #0b1622; color: #fff; border: 2px solid #4d6f96; border-radius: 12px;
}
.form input[type=color] { width: 100%; height: 52px; border: 2px solid #4d6f96; border-radius: 12px; background: #0b1622; padding: 4px; }
.form .err { color: #ff8a80; font-weight: 700; min-height: 1.4em; margin-top: 8px; }

/* Tamaño de elementos manuales */
.manual-actions .lbl { width: 100%; margin: 6px 0 0; }
.tam-seg { width: 100%; margin-bottom: 0; }
/* PC: edición con mouse (arrastrar posición y tamaño) */
body:not(.pc) .solo-pc, body.pc .solo-touch { display: none !important; }
.pc-edit { width: 100%; display: flex; flex-direction: column; gap: 6px; }
#overlay .edit-area { pointer-events: all; cursor: move; fill: transparent; stroke: #000; stroke-width: 2; stroke-dasharray: 4 3; vector-effect: non-scaling-stroke; }
#overlay .handle { pointer-events: all; fill: #fff; stroke: #000; stroke-width: 2; vector-effect: non-scaling-stroke; }
#overlay .handle.nw, #overlay .handle.se { cursor: nwse-resize; }
#overlay .handle.ne, #overlay .handle.sw { cursor: nesw-resize; }
body.arrastrando, body.arrastrando * { cursor: grabbing !important; }
/* PC: la ficha es un panel lateral derecho para no tapar el plano mientras se edita */
@media (min-width: 900px) {
  body.pc .sheet {
    left: auto; right: 0; top: calc(var(--sat) + var(--bar-h)); bottom: 0; margin: 0;
    width: 440px; max-width: 440px; max-height: none;
    border-radius: 16px 0 0 0; border-bottom: 2px solid #3a5a80; border-right: 0;
    transform: translateX(105%);
  }
  body.pc .sheet.open { transform: translateX(0); }
  body.pc.ficha-abierta .mapbtns { right: calc(var(--sar) + 456px); }
  body.pc .sheet .grip { display: none; }
}
/* Los avisos no bloquean toques y suben por encima de la barra del visor */
.toast { pointer-events: none; }
#viewer:not([hidden]) ~ #toast { bottom: calc(var(--sab) + 110px); }
/* celular/tablet táctil: sin botones de zoom (se usa pellizcar) ni agregar/mover elementos (se hace en el PC) */
body:not(.pc) .mapbtns, body:not(.pc) #btnAgregar, #btnMover { display: none !important; }
/* "Más opciones" de la ficha: discreto, plegado */
.mas-opciones { margin-top: 18px; border-top: 1px solid rgba(255,255,255,.08); padding-top: 8px; }
.mas-opciones > summary { cursor: pointer; color: #8a8f98; font-size: 14px; padding: 8px 0; list-style: none; user-select: none; }
.mas-opciones > summary::-webkit-details-marker { display: none; }
.mas-opciones > summary::before { content: '▸ '; }
.mas-opciones[open] > summary::before { content: '▾ '; }
.mas-opciones .btn { margin-top: 6px; }
.viewer-bar .btn { white-space: nowrap; }
.sync-opts { display: flex; gap: 8px; margin: 6px 0 10px; }
.sync-opts .btn { flex: 1; font-size: 14px; min-height: 40px; }
.thumb.remota img:not([src]) { opacity: 0; }
/* cámara rápida */
.photo-actions { flex-wrap: wrap; }
.cam-rapida { flex-basis: 100%; }
body:not(.pc) .cam-nativa { background: var(--panel2); color: inherit; border-color: transparent; font-size: 17px; min-height: 56px; }
body:not(.pc) .cam-nativa ~ .btn.huge { font-size: 17px; min-height: 56px; }
.camara { position: fixed; inset: 0; z-index: 70; background: #000; touch-action: manipulation; }
.camara video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; }
.cam-flash { position: absolute; inset: 0; background: #fff; opacity: 0; pointer-events: none; }
.cam-flash.on { animation: camflash .18s ease-out; }
@keyframes camflash { from { opacity: .75; } to { opacity: 0; } }
.cam-top { position: absolute; top: 0; left: 0; right: 0; display: flex; gap: 8px; align-items: center; padding: calc(var(--sat) + 10px) calc(var(--sar) + 12px) 10px calc(var(--sal) + 12px); background: linear-gradient(rgba(0,0,0,.6), transparent); }
.cam-titulo { flex: 1; font-weight: 800; font-size: 18px; text-shadow: 0 1px 3px #000; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cam-chip { min-width: 46px; height: 46px; border-radius: 23px; border: 2px solid rgba(255,255,255,.7); background: rgba(0,0,0,.45); color: #fff; font-size: 20px; font-weight: 700; }
.cam-chip.on { background: #ffd60a; color: #000; }
.cam-bottom { position: absolute; left: 0; right: 0; bottom: 0; display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 10px; padding: 16px calc(var(--sar) + 14px) calc(var(--sab) + 18px) calc(var(--sal) + 14px); background: linear-gradient(transparent, rgba(0,0,0,.7)); }
.cam-tira { display: flex; gap: 4px; overflow: hidden; min-height: 54px; align-items: center; }
.cam-mini { height: 54px; width: auto; border-radius: 6px; border: 2px solid #fff; flex: none; }
.cam-mini:not(:first-child) { height: 40px; opacity: .8; }
.cam-disparo { width: 84px; height: 84px; border-radius: 50%; background: #fff; border: 6px solid rgba(255,255,255,.45); background-clip: padding-box; box-shadow: 0 0 0 3px #fff; }
.cam-disparo:active { transform: scale(.9); background: #ddd; }
.cam-der { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }
.cam-der .btn { min-height: 48px; font-size: 17px; white-space: nowrap; }
.cam-estado { font-size: 13px; color: #ddd; min-height: 16px; }
/* GPS beta */
.gps-sec { border: 1px solid rgba(10,132,255,.35); border-radius: 12px; padding: 10px; margin: 10px 0; }
.gps-sec .btn.on { background: #0a84ff; color: #fff; }
.gps-fab { position: absolute; z-index: 5; right: calc(var(--sar) + 12px); bottom: calc(var(--sab) + 16px); color: #0a84ff; font-size: 30px; }
#overlay .gps-cal { fill: #ff9f0a; stroke: #fff; }
#overlay .gps-cal-t { fill: #fff; font-weight: 700; paint-order: stroke; stroke: #000; }
#overlay .gps-acc { fill: rgba(10,132,255,.15); stroke: rgba(10,132,255,.6); }
#overlay .gps-dot { fill: #0a84ff; stroke: #fff; }
#overlay .gps-dot.viejo { fill: #8e8e93; }
