:root {
  /* Colores base y variables dinamicas controladas desde app.js. */
  --ink: #18222e;
  --muted: #657282;
  --line: #d7dee8;
  --surface: #f6f8fb;
  --panel: #ffffff;
  --camera: #09a66d;
  --network: #0b86c6;
  --nvr: #7a4fd3;
  --antenna: #c04a8f;
  --switch: #4766d6;
  --alarm: #d4513c;
  --device: #e09b20;
  --zoom: 0.62;
  --pan-x: 0px;
  --pan-y: 0px;
  color-scheme: light;
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

/* Reset minimo para mantener medidas predecibles. */
* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
  margin: 0;
}

body {
  background: var(--surface);
  color: var(--ink);
}

/* Oculta el plano hasta que el servidor confirme una sesion valida. */
body.auth-pending .app-shell,
body.auth-pending .tooltip {
  visibility: hidden;
}

/* La pantalla de login solo aparece en index.html. */
body:not(.login-page) .login-screen {
  display: none;
}

/* Overlay de login con tarjeta centrada. */
.login-screen {
  display: grid;
  min-height: 100vh;
  align-items: center;
  justify-items: end;
  padding: 32px 7vw 32px 24px;
  background:
    linear-gradient(rgba(8, 14, 25, 0.1), rgba(8, 14, 25, 0.34)),
    url("login-background.png") center / cover no-repeat;
}

.login-card {
  display: grid;
  width: min(420px, 100%);
  gap: 14px;
  padding: 24px;
  border: 1px solid rgba(255, 255, 255, 0.36);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.92);
  box-shadow: 0 20px 55px rgba(2, 9, 20, 0.36);
  backdrop-filter: blur(10px);
}

.login-card label {
  display: grid;
  gap: 6px;
  color: #4a596b;
  font-size: 12px;
  font-weight: 750;
}

.login-card input {
  min-height: 40px;
  padding: 0 11px;
  border: 1px solid #c9d3df;
  border-radius: 8px;
}

.login-error {
  min-height: 18px;
  margin: 0;
  color: #a33b2d;
  font-size: 13px;
  font-weight: 750;
}

.login-help {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
}

/* Elementos visibles solo para rol administrador. */
body:not(.role-admin) .admin-only {
  display: none;
}

/* Hereda la tipografia en todos los controles nativos. */
button,
input,
select,
textarea {
  font: inherit;
}

button {
  min-height: 36px;
  border: 1px solid #b8c4d2;
  border-radius: 8px;
  background: #ffffff;
  color: var(--ink);
  cursor: pointer;
}

button:hover {
  border-color: #60768e;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.52;
}

.login-card button {
  min-height: 42px;
  background: #18222e;
  color: #fff;
}

/* Layout general: visor del plano + panel administrador. */
.app-shell {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 390px;
  height: 100vh;
  overflow: hidden;
}

/* Columna izquierda: barra superior y mapa. */
.map-area {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  min-width: 0;
}

/* Cabecera con titulo y herramientas de plano. */
.topbar,
.side-panel {
  background: rgba(255, 255, 255, 0.96);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
}

.eyebrow {
  margin: 0 0 3px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

h1,
h2 {
  margin: 0;
  letter-spacing: 0;
}

h1 {
  font-size: 22px;
}

h2 {
  font-size: 18px;
}

/* Barra de herramientas e interacciones compactas. */
.toolbar,
.bulk-actions,
.position-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.toolbar {
  flex-wrap: wrap;
  justify-content: flex-end;
}

.toolbar button,
.bulk-actions button,
.file-button {
  padding: 0 12px;
}

.zoom-control,
.switch,
.type-control {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 36px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: #344253;
  font-size: 13px;
}

.zoom-control input {
  width: 110px;
}

.type-control select {
  width: 112px;
  min-height: 28px;
  border: 0;
  background: transparent;
  color: var(--ink);
  outline: 0;
}

.role-badge {
  display: inline-flex;
  align-items: center;
  min-height: 36px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #eef4fb;
  color: #25364b;
  font-size: 12px;
  font-weight: 800;
}

/* Ventana visible del plano. El plano se mueve dentro con transform. */
.map-viewport {
  position: relative;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  padding: 18px;
  background:
    linear-gradient(90deg, rgba(24, 34, 46, 0.05) 1px, transparent 1px),
    linear-gradient(rgba(24, 34, 46, 0.05) 1px, transparent 1px);
  background-size: 32px 32px;
  cursor: grab;
  touch-action: none;
  overscroll-behavior: contain;
}

/* Estado mientras el usuario arrastra el plano. */
.map-viewport.panning {
  cursor: grabbing;
  user-select: none;
}

/* Escenario del plano: tamano escalado y traslacion panX/panY. */
.plan-stage {
  position: relative;
  width: calc(2420px * var(--zoom));
  height: calc(1870px * var(--zoom));
  min-width: 360px;
  min-height: 278px;
  box-shadow: 0 10px 30px rgba(34, 47, 64, 0.16);
  background: #fff;
  transform: translate(var(--pan-x), var(--pan-y));
  transform-origin: top left;
  will-change: transform;
}

/* Imagen base del PDF: no captura mouse para permitir arrastre fluido. */
.plan-stage img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
  user-select: none;
}

/* Capa de marcadores superpuesta a la imagen. */
.marker-layer {
  position: absolute;
  inset: 0;
}

/* Marcador base: posicion porcentual, etiqueta y punto clickeable. */
.marker {
  position: absolute;
  left: var(--x);
  top: var(--y);
  display: grid;
  place-items: center;
  min-width: 26px;
  min-height: 0;
  height: 26px;
  padding: 0 6px;
  transform: translate(-50%, -50%);
  border: 2px solid #ffffff;
  border-radius: 999px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.36);
  color: #ffffff;
  font-size: 11px;
  font-weight: 850;
  line-height: 1;
  white-space: nowrap;
}

/* Area tactil ampliada del marcador sin agrandar el circulo visible. */
.marker::after {
  content: "";
  position: absolute;
  inset: -6px;
  border-radius: inherit;
}

/* Colores por tipo de marcador. */
.marker.camera {
  background: var(--camera);
}

.marker.network {
  background: var(--network);
}

.marker.nvr {
  background: var(--nvr);
}

.marker.antenna {
  background: var(--antenna);
}

.marker.switch {
  background: var(--switch);
}

.marker.alarm {
  background: var(--alarm);
}

.marker.device {
  background: var(--device);
}

.marker.selected {
  outline: 3px solid #111827;
  outline-offset: 2px;
  z-index: 4;
}

/* Vista compacta cuando se ocultan etiquetas. */
.marker.hide-label {
  min-width: 18px;
  width: 18px;
  height: 18px;
  padding: 0;
  color: transparent;
}

/* Panel derecho: lista arriba, formulario abajo, con scroll general visible. */
.side-panel {
  height: 100vh;
  height: 100dvh;
  min-height: 0;
  border-left: 1px solid var(--line);
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-gutter: stable;
}

/* Bloques internos del panel lateral. */
.panel-section {
  padding: 16px;
  border-bottom: 1px solid var(--line);
}

/* La lista se limita para no empujar la ficha tecnica fuera de pantalla. */
.side-panel > .panel-section:first-child {
  max-height: min(38dvh, 360px);
  overflow-y: auto;
  scrollbar-gutter: stable;
}

/* La ficha puede crecer; el scroll lo maneja la barra lateral completa. */
.editor-section {
  padding-bottom: 28px;
}

/* Encabezados internos del panel. */
.panel-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

/* Contador de marcas. */
.count-pill {
  min-width: 34px;
  padding: 5px 9px;
  border-radius: 999px;
  background: #e6eef7;
  color: #25364b;
  text-align: center;
  font-size: 12px;
  font-weight: 800;
}

/* Inputs principales del buscador y formulario. */
.search,
.marker-form input,
.marker-form select,
.marker-form textarea {
  width: 100%;
  border: 1px solid #c9d3df;
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
}

.search {
  min-height: 38px;
  padding: 0 11px;
  margin-bottom: 10px;
}

/* Acciones de importar/exportar/restaurar. */
.bulk-actions {
  margin-bottom: 12px;
}

/* Boton visual que envuelve el input file nativo. */
.file-button {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 36px;
  border: 1px solid #b8c4d2;
  border-radius: 8px;
  background: #fff;
  cursor: pointer;
}

.file-button input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

/* Lista vertical de marcas detectadas/agregadas. */
.marker-list {
  display: grid;
  gap: 7px;
}

/* Item seleccionable de la lista lateral. */
.list-item {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 10px;
  width: 100%;
  min-height: 46px;
  padding: 8px 10px;
  border: 1px solid #d7dee8;
  border-radius: 8px;
  background: #fff;
  text-align: left;
}

.list-item.active {
  border-color: #1f2a37;
  box-shadow: 0 0 0 2px rgba(31, 42, 55, 0.1);
}

/* Punto de color que replica el tipo visual del marcador. */
.dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--device);
}

.dot.camera {
  background: var(--camera);
}

.dot.network {
  background: var(--network);
}

.dot.nvr {
  background: var(--nvr);
}

.dot.antenna {
  background: var(--antenna);
}

.dot.switch {
  background: var(--switch);
}

.dot.alarm {
  background: var(--alarm);
}

/* Texto dentro de cada fila de lista. */
.list-text {
  min-width: 0;
}

.list-title,
.list-meta {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.list-title {
  font-weight: 800;
}

.list-meta {
  color: var(--muted);
  font-size: 12px;
}

/* Indicador de que la marca tiene link cargado. */
.link-state {
  color: #0b6d4a;
  font-size: 12px;
  font-weight: 800;
}

/* Boton de accion destructiva. */
.danger {
  border-color: #e0b0a8;
  color: #a33b2d;
}

/* Formulario de ficha tecnica. */
.marker-form {
  display: grid;
  gap: 11px;
}

.marker-form label {
  display: grid;
  gap: 5px;
  color: #4a596b;
  font-size: 12px;
  font-weight: 750;
}

.marker-form input,
.marker-form select {
  min-height: 38px;
  padding: 0 10px;
  font-size: 14px;
  font-weight: 500;
}

.marker-form textarea {
  padding: 9px 10px;
  resize: vertical;
  font-size: 14px;
  font-weight: 500;
}

/* Grillas compactas para campos relacionados. */
.two-col,
.position-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 9px;
}

.position-row {
  grid-template-columns: 1fr 1fr auto;
  align-items: end;
}

/* Tooltip tecnico al pasar el mouse sobre una marca. */
.tooltip {
  position: fixed;
  z-index: 20;
  display: none;
  max-width: 320px;
  padding: 10px 12px;
  border: 1px solid #263241;
  border-radius: 8px;
  background: rgba(18, 25, 34, 0.96);
  color: #fff;
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.3);
  pointer-events: none;
}

.tooltip strong {
  display: block;
  margin-bottom: 7px;
  font-size: 13px;
}

.tooltip dl {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 10px;
  margin: 0;
  font-size: 12px;
}

.tooltip dt {
  color: #b9c6d7;
}

.tooltip dd {
  margin: 0;
  overflow-wrap: anywhere;
}

/* Estado visual al elegir "Agregar" y esperar click en el plano. */
body.adding .map-viewport {
  cursor: crosshair;
}

/* Version responsive: apila plano y panel en pantallas angostas. */
@media (max-width: 980px) {
  .app-shell {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(48vh, 1fr) minmax(0, 52vh);
  }

  .side-panel {
    height: auto;
    min-height: 0;
    overflow-y: auto;
    border-top: 1px solid var(--line);
    border-left: 0;
  }

  .side-panel > .panel-section:first-child {
    max-height: 32vh;
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .toolbar {
    justify-content: flex-start;
  }
}
