:root {
  --bg: #0f1720;
  --surface: #16222e;
  --surface-2: #1d2c3b;
  --line: #2a3d50;
  --text: #e8eef4;
  --muted: #8ba3b8;
  --accent: #ffb03a;
  --danger: #e5484d;
  --radius: 12px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; margin: 0; }
html, body { height: 100%; overflow: hidden; }
body { background: var(--bg); color: var(--text); }

.hidden { display: none !important; }

.screen { display: flex; flex-direction: column; height: 100dvh; }

/* ---------- Barra superior ---------- */
.topbar {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 14px; background: var(--surface);
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}
.topbar h1 {
  font-size: 17px; font-weight: 700; letter-spacing: .2px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 1;
}
.tag {
  font-size: 11px; text-transform: uppercase; letter-spacing: 1.5px;
  color: var(--accent); border: 1px solid var(--accent);
  padding: 2px 8px; border-radius: 999px;
}
.topbar-actions { display: flex; gap: 6px; }

.icon-btn {
  background: none; border: none; color: var(--text);
  font-size: 20px; padding: 6px 10px; cursor: pointer; border-radius: 8px;
}
.icon-btn:active { background: var(--surface-2); }

/* ---------- Lista de sitios ---------- */
.sites-body { padding: 16px; overflow-y: auto; flex: 1; }
.card {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 16px;
  display: flex; flex-direction: column; gap: 12px;
}
.card h2 { font-size: 15px; }
.section-title { font-size: 13px; color: var(--muted); margin: 20px 0 10px; text-transform: uppercase; letter-spacing: 1px; }

input[type="text"], input[type="number"], select {
  width: 100%; padding: 12px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--surface-2);
  color: var(--text); font-size: 16px;
}
label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }

.file-btn {
  display: block; text-align: center; padding: 12px;
  border: 1px dashed var(--line); border-radius: 10px;
  color: var(--muted); cursor: pointer; font-size: 14px;
}
.file-btn.small { padding: 10px; }
.file-btn input { display: none; }

.btn {
  padding: 12px 18px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--surface-2); color: var(--text);
  font-size: 15px; font-weight: 600; cursor: pointer; text-align: center;
  text-decoration: none; display: inline-block;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: #1a1204; }
.btn.danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn:active { filter: brightness(1.15); }

.sites-list { display: flex; flex-direction: column; gap: 10px; }
.site-item {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 14px;
  display: flex; align-items: center; gap: 12px; cursor: pointer;
}
.site-item .info { flex: 1; }
.site-item .name { font-weight: 600; }
.site-item .meta { font-size: 12px; color: var(--muted); margin-top: 2px; }
.site-item .del { color: var(--danger); background: none; border: none; font-size: 18px; padding: 8px; cursor: pointer; }

/* ---------- Visor ---------- */
#viewport {
  flex: 1; position: relative; overflow: hidden;
  background: #0a0f15; touch-action: none;
}
#world { position: absolute; transform-origin: 0 0; }
#pdf-canvas { display: block; background: #fff; box-shadow: 0 0 40px rgba(0,0,0,.6); }
#overlay { position: absolute; inset: 0; }

#add-hint {
  position: absolute; top: 12px; left: 50%; transform: translateX(-50%);
  background: var(--accent); color: #1a1204; font-size: 13px; font-weight: 600;
  padding: 8px 14px; border-radius: 999px; pointer-events: none; z-index: 5;
}

/* Zonas sobre el plano */
.zone {
  position: absolute; border: 2.5px solid; border-radius: 4px;
  background: rgba(255,255,255,0.06); cursor: grab; touch-action: none;
}
.zone.selected { box-shadow: 0 0 0 3px rgba(255,255,255,.5); z-index: 3; }
.zone .zlabel {
  position: absolute; top: -1.8em; left: 0;
  font-size: 12px; font-weight: 700; white-space: nowrap;
  padding: 1px 7px; border-radius: 6px; color: #fff;
  text-shadow: 0 1px 2px rgba(0,0,0,.6);
}
.zone { --ctrl: 26px; }
.zone .zdot {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%,-50%);
  width: calc(var(--ctrl) * .4); height: calc(var(--ctrl) * .4); border-radius: 50%;
  border: 2px solid #fff; box-shadow: 0 1px 3px rgba(0,0,0,.5);
}
.zone .zhandle {
  position: absolute; right: calc(var(--ctrl) / -2); bottom: calc(var(--ctrl) / -2);
  width: var(--ctrl); height: var(--ctrl); border-radius: 50%;
  border: 2px solid #fff; cursor: nwse-resize; touch-action: none;
}
.zone .zedit {
  position: absolute; left: calc(var(--ctrl) / -2); top: calc(var(--ctrl) / -2);
  width: var(--ctrl); height: var(--ctrl); border-radius: 50%;
  border: 2px solid #fff; background: var(--surface);
  color: #fff; font-size: calc(var(--ctrl) * .5); line-height: calc(var(--ctrl) - 4px);
  text-align: center; cursor: pointer; overflow: hidden;
}
/* En zonas diminutas, oculta la etiqueta para no tapar el cuadro */
.zone.tiny .zlabel { display: none; }
.zone.tiny.selected .zlabel { display: block; }

/* ---------- Barra inferior ---------- */
.toolbar {
  display: flex; gap: 8px; align-items: center;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  background: var(--surface); border-top: 1px solid var(--line);
  flex-shrink: 0;
}
.tool-btn {
  min-width: 46px; padding: 10px 14px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--surface-2);
  color: var(--text); font-size: 17px; font-weight: 700; cursor: pointer;
}
.tool-btn.accent { background: var(--accent); border-color: var(--accent); color: #1a1204; flex: 1; font-size: 15px; }
.tool-btn.accent.active { background: #fff; }
.page-nav { display: flex; align-items: center; gap: 6px; }
#page-label { font-size: 13px; color: var(--muted); }

/* ---------- Panel lateral ---------- */
.panel {
  position: absolute; top: 0; right: 0; bottom: 0;
  width: min(340px, 88vw); background: var(--surface);
  border-left: 1px solid var(--line); z-index: 20;
  display: flex; flex-direction: column;
  box-shadow: -10px 0 30px rgba(0,0,0,.5);
}
.panel-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px; border-bottom: 1px solid var(--line);
}
.panel-head h2 { font-size: 15px; }
.panel-list { overflow-y: auto; flex: 1; padding: 10px; display: flex; flex-direction: column; gap: 8px; }
.panel-item {
  display: flex; gap: 10px; align-items: center;
  background: var(--surface-2); border: 1px solid var(--line);
  border-radius: 10px; padding: 10px 12px; cursor: pointer;
}
.panel-item .pi-info { flex: 1; min-width: 0; }
.panel-item .pi-name { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.panel-item .pi-meta { font-size: 12px; color: var(--muted); }
.color-dot { width: 14px; height: 14px; border-radius: 50%; flex-shrink: 0; display: inline-block; }

/* ---------- Modales ---------- */
.modal {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(5,10,15,.7); backdrop-filter: blur(2px);
  display: flex; align-items: flex-end; justify-content: center;
}
.modal-box {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 16px 16px 0 0; width: 100%; max-width: 520px;
  max-height: 88dvh; overflow-y: auto;
  padding: 18px 18px calc(18px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 14px;
}
.modal-box.wide { max-width: 720px; }
.modal-head { display: flex; align-items: center; gap: 10px; }
.modal-head h2 { flex: 1; font-size: 16px; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 4px; }
.hint { font-size: 13px; color: var(--accent); }
.field-label { font-size: 13px; color: var(--muted); }

.photos-row { display: flex; gap: 8px; flex-wrap: wrap; }
.photo-thumb { position: relative; }
.photo-thumb img {
  width: 84px; height: 84px; object-fit: cover;
  border-radius: 8px; border: 1px solid var(--line);
}
.photo-thumb .pdel {
  position: absolute; top: -6px; right: -6px;
  width: 22px; height: 22px; border-radius: 50%;
  background: var(--danger); color: #fff; border: none;
  font-size: 12px; cursor: pointer;
}

/* ---------- Informe ---------- */
.report-totals { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.rt-box {
  flex: 1; min-width: 110px; background: var(--surface-2);
  border: 1px solid var(--line); border-radius: 10px; padding: 12px; text-align: center;
}
.rt-box .num { font-size: 22px; font-weight: 800; color: var(--accent); }
.rt-box .lbl { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; }
.report-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.report-table th, .report-table td {
  padding: 8px 6px; border-bottom: 1px solid var(--line); text-align: left;
}
.report-table th { color: var(--muted); font-weight: 600; font-size: 11px; text-transform: uppercase; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; bottom: 90px; left: 50%; transform: translateX(-50%);
  background: var(--surface-2); border: 1px solid var(--line);
  color: var(--text); padding: 10px 18px; border-radius: 999px;
  font-size: 14px; z-index: 100; box-shadow: 0 4px 20px rgba(0,0,0,.5);
}

/* ---------- Grupos de riel (cantidad ilimitada) ---------- */
.section-block { display: flex; flex-direction: column; gap: 10px; }
.field-hint { color: var(--muted); font-weight: 400; font-size: 12px; }
#rail-groups { display: flex; flex-direction: column; gap: 10px; }
.railg-row {
  display: flex; flex-direction: column; gap: 6px;
  border: 1px solid var(--line); border-radius: 10px;
  padding: 8px; background: var(--surface-2);
}
.railg-main { display: flex; align-items: center; gap: 8px; }
.railg-main input { flex: 1; min-width: 0; padding: 10px; }
.railg-main .railg-x { color: var(--muted); font-size: 15px; }
.railg-prod { width: 100%; padding: 8px 10px; font-size: 14px; }
.railg-del {
  flex-shrink: 0; width: 34px; height: 40px; border-radius: 8px;
  background: transparent; border: 1px solid var(--line);
  color: var(--danger); font-size: 14px; cursor: pointer;
}

/* Zona bloqueada por otra persona (colaboracion en vivo) */
.panel-item.locked { opacity: .55; }
.panel-item.locked .icon-btn { cursor: not-allowed; }
.lock-badge {
  font-size: 10px; font-weight: 700; color: #22c55e;
  border: 1px solid #22c55e; border-radius: 999px; padding: 0 6px; margin-left: 4px;
  white-space: nowrap;
}
.btn.ghost { background: transparent; border: 1px dashed var(--line); color: var(--muted); font-weight: 600; }
.btn.small { padding: 8px 12px; font-size: 13px; }

/* ---------- Presencia en vivo ---------- */
.presence {
  display: inline-flex; align-items: center; gap: 5px;
  background: var(--surface-2); border: 1px solid var(--line);
  color: var(--text); font-size: 13px; font-weight: 700;
  padding: 4px 10px; border-radius: 999px;
}
.presence-dot {
  width: 9px; height: 9px; border-radius: 50%;
  background: #22c55e; box-shadow: 0 0 0 0 rgba(34,197,94,.6);
  animation: presence-pulse 1.6s infinite;
}
@keyframes presence-pulse {
  0%   { box-shadow: 0 0 0 0 rgba(34,197,94,.6); }
  70%  { box-shadow: 0 0 0 7px rgba(34,197,94,0); }
  100% { box-shadow: 0 0 0 0 rgba(34,197,94,0); }
}

/* Zona que otra persona esta editando ahora */
.zone.busy { animation: zone-busy 1.4s ease-in-out infinite; }
@keyframes zone-busy {
  0%, 100% { box-shadow: 0 0 0 2px rgba(34,197,94,.15); }
  50%      { box-shadow: 0 0 0 4px rgba(34,197,94,.55); }
}

/* ---------- Foto subiendo (previsualizacion) ---------- */
.photo-thumb.uploading img { filter: brightness(.55); }
.up-overlay {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  border-radius: 8px;
}
.spinner {
  width: 22px; height: 22px; border-radius: 50%;
  border: 3px solid rgba(255,255,255,.35); border-top-color: #fff;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

@media (min-width: 700px) {
  .modal { align-items: center; }
  .modal-box { border-radius: 16px; }
  .sites-body { max-width: 640px; margin: 0 auto; width: 100%; }
}
