:root {
  --bg: #071523;
  --bg-panel: rgba(13, 24, 37, 0.9);
  --panel-soft: rgba(19, 31, 46, 0.95);
  --line: rgba(148, 163, 184, 0.18);
  --text: #edf6ff;
  --muted: #9ec0ec;
  --primary: #3cc7c6;
  --primary-2: #5da7ff;
  --success: #25d19b;
  --danger: #ef5b5b;
  --warning: #f5b84b;
  --shadow: 0 18px 40px rgba(2, 6, 12, 0.42);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, sans-serif;
  --content-pad: 20px;
  --topbar-h: 56px;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  min-height: 100%;
  font-family: var(--font);
  background: radial-gradient(circle at top, rgba(93, 167, 255, 0.14), transparent 25%),
    linear-gradient(135deg, #010b14 0%, #071523 35%, #0b1828 100%);
  background-color: var(--bg);
  background-attachment: fixed;
  color: var(--text);
}

body {
  min-height: 100vh;
}

button,
input,
select {
  font: inherit;
}

h2 {
  margin: 0 0 18px;
  font-size: 1.1rem;
}

.muted {
  color: var(--muted);
  font-size: 0.8rem;
}

.hint {
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 400;
}

.hint.warning {
  color: var(--warning);
}

.empty {
  color: var(--muted);
  margin: 0;
}

.mono {
  font-family: ui-monospace, Consolas, monospace;
}

/* --- Login ---------------------------------------------------------------- */

.login-screen {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 16px;
}

.login-card {
  width: 100%;
  max-width: 380px;
  padding: 28px;
  display: grid;
  gap: 16px;
}

.login-card h1 {
  margin: 0 0 8px;
}

/* --- Estructura: menú lateral (PC) / barra superior (celular y tablet) ----- */

.app-shell {
  display: grid;
  grid-template-columns: 230px minmax(0, 1fr);
  min-height: 100vh;
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  background: rgba(10, 20, 31, 0.88);
  border-right: 1px solid var(--line);
  padding: 22px 16px;
  display: flex;
  flex-direction: column;
  gap: 22px;
  z-index: 15;
}

.brand-block {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 6px;
}

.brand-text {
  flex: 1;
  min-width: 0;
}

.brand-kicker {
  margin: 0 0 6px;
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.14em;
  color: var(--primary);
}

.brand-block h1 {
  margin: 0;
  font-size: 1.5rem;
  white-space: nowrap;
}

.status-dot,
.icon-button {
  display: none;
}

.status-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--muted);
  flex: none;
}

.status-dot.online {
  background: var(--success);
  box-shadow: 0 0 8px rgba(37, 209, 155, 0.7);
}

.status-dot.offline {
  background: var(--danger);
}

.icon-button {
  width: 38px;
  height: 38px;
  flex: none;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  padding: 0;
  place-items: center;
}

.icon-button svg {
  width: 22px;
  height: 22px;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  fill: none;
}

.sidebar-menu {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 22px;
  min-height: 0;
}

.nav {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.nav-item {
  color: var(--text);
  text-decoration: none;
  border: 1px solid transparent;
  border-radius: 12px;
  padding: 11px 14px;
  transition: 0.2s ease;
}

.nav-item.active,
.nav-item:hover {
  border-color: rgba(60, 199, 198, 0.5);
  background: rgba(60, 199, 198, 0.08);
}

.sidebar-footer {
  margin-top: auto;
  display: grid;
  gap: 10px;
}

.user-chip {
  color: var(--muted);
  font-size: 0.85rem;
  text-align: center;
}

.content {
  padding: var(--content-pad);
  min-width: 0;
}

.view {
  display: none;
}

.view.active {
  display: block;
}

.panel {
  background: var(--bg-panel);
  border: 1px solid var(--line);
  border-radius: 20px;
  box-shadow: var(--shadow);
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.panel-header h2 {
  margin: 0;
}

.quick-actions {
  padding: 18px 20px;
  margin-bottom: 20px;
}

.two-columns {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 24px;
  align-items: start;
}

.two-columns.wide-left {
  grid-template-columns: minmax(0, 1.6fr) minmax(280px, 1fr);
}

.stacked-gap {
  margin-bottom: 24px;
}

/* --- Botones -------------------------------------------------------------- */

.button-row {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
}

.action-button {
  border: none;
  border-radius: 12px;
  padding: 12px 18px;
  color: white;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.2s ease, opacity 0.2s ease;
}

.action-button.small {
  padding: 7px 12px;
  font-size: 0.82rem;
  white-space: nowrap;
}

.action-button:hover:not(:disabled) {
  transform: translateY(-1px);
}

button:disabled {
  opacity: 0.55;
  cursor: wait;
}

.success {
  background: linear-gradient(135deg, var(--success), #15b58f);
}

.danger {
  background: linear-gradient(135deg, var(--danger), #d9465f);
}

.neutral {
  background: linear-gradient(135deg, var(--primary-2), #2d6fe8);
}

.ghost {
  background: transparent;
  border: 1px solid var(--line);
}

.link-button {
  background: none;
  border: none;
  color: var(--primary);
  cursor: pointer;
  padding: 4px 6px;
}

.danger-text {
  color: #ff9a9a;
}

/* --- Estado --------------------------------------------------------------- */

.status-pill {
  background: rgba(60, 199, 198, 0.12);
  border: 1px solid rgba(60, 199, 198, 0.28);
  border-radius: 999px;
  padding: 9px 14px;
  text-align: center;
  font-weight: 700;
}

.status-pill.online {
  background: rgba(37, 209, 155, 0.12);
  border-color: rgba(37, 209, 155, 0.38);
  color: #baf7df;
}

.status-pill.offline {
  background: rgba(239, 91, 91, 0.1);
  border-color: rgba(239, 91, 91, 0.3);
  color: #ffc4c4;
}

.status-tag {
  min-width: 62px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 999px;
  padding: 6px 10px;
  font-size: 0.7rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  white-space: nowrap;
}

.status-tag.on {
  background: rgba(37, 209, 155, 0.12);
  border: 1px solid rgba(37, 209, 155, 0.3);
  color: #baf7df;
}

.status-tag.off {
  background: rgba(239, 91, 91, 0.12);
  border: 1px solid rgba(239, 91, 91, 0.24);
  color: #ffc4c4;
}

/* --- Panel de control: acciones + cámaras --------------------------------- */

/* Horizontal (PC, tablet o celular apaisado): acciones a la izquierda. */
.dashboard.active {
  display: grid;
  grid-template-columns: 270px minmax(0, 1fr);
  gap: 16px;
  align-items: start;
}

.dashboard.no-cameras.active {
  grid-template-columns: minmax(0, 1fr);
}

.controls-panel {
  padding: 14px;
  display: grid;
  gap: 12px;
}

.controls-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.controls-header h2 {
  margin: 0;
  font-size: 1rem;
}

.quick-buttons {
  display: flex;
  gap: 6px;
}

.controls-panel .hint {
  margin: 0;
}

.device-list {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 8px;
}

.dashboard.no-cameras .device-list {
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
}

.device-tile {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  width: 100%;
  min-height: 52px;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--panel-soft);
  color: var(--text);
  text-align: left;
  cursor: pointer;
  transition: background 0.2s ease, border-color 0.2s ease, transform 0.1s ease;
  -webkit-tap-highlight-color: transparent;
}

div.device-tile {
  cursor: default;
}

button.device-tile:hover:not(:disabled) {
  border-color: rgba(60, 199, 198, 0.5);
}

button.device-tile:active:not(:disabled) {
  transform: scale(0.98);
}

.device-tile.is-on {
  background: linear-gradient(135deg, rgba(37, 209, 155, 0.9), rgba(21, 181, 143, 0.9));
  border-color: rgba(37, 209, 155, 0.9);
  color: #04261c;
}

.device-tile.is-busy {
  opacity: 0.7;
}

.device-icon {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.device-name {
  font-weight: 700;
  font-size: 0.95rem;
  line-height: 1.2;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.device-state {
  font-size: 0.7rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  padding: 4px 8px;
  border-radius: 999px;
  background: rgba(239, 91, 91, 0.14);
  color: #ffc4c4;
}

.device-tile.is-on .device-state {
  background: rgba(4, 38, 28, 0.25);
  color: #04261c;
}

/* Grilla de cámaras: columnas y ancho máximo los calcula cameras.js (fit). */
.camera-grid {
  display: grid;
  grid-template-columns: repeat(var(--cols, 2), minmax(0, var(--tile-width, 1fr)));
  justify-content: center;
  align-content: start;
  gap: 8px;
}

.camera-tile {
  position: relative;
  margin: 0;
  aspect-ratio: 16 / 9;
  background: #000;
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
  cursor: pointer;
}

.camera-tile:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

.camera-tile video {
  width: 100%;
  height: 100%;
  display: block;
  background: #000;
  /* El substream CIF del DVR es "angosto": se estira a 16:9 (proporción real). */
  object-fit: fill;
}

.camera-tile figcaption {
  position: absolute;
  left: 6px;
  bottom: 6px;
  max-width: calc(100% - 12px);
  padding: 2px 8px;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.6);
  font-size: 0.75rem;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.camera-overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 12px;
  text-align: center;
  color: var(--muted);
  font-size: 0.8rem;
  background: rgba(1, 11, 20, 0.6);
  pointer-events: none;
}

/* --- Visor a pantalla completa -------------------------------------------- */

.viewer {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: flex;
  flex-direction: column;
  background: #000;
}

.viewer-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 14px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--line);
}

.viewer-bar .button-row {
  gap: 8px;
  flex-wrap: nowrap;
}

.viewer-stage {
  position: relative;
  flex: 1;
  min-height: 0;
  background: #000;
}

.viewer-stage video {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: contain;
  background: #000;
}

/* --- Formularios y tablas ------------------------------------------------- */

.form-panel,
.table-panel {
  padding: 22px;
}

.form-grid {
  display: grid;
  gap: 14px;
}

.inline-fields {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 120px;
  gap: 12px;
}

label {
  display: grid;
  gap: 8px;
  font-weight: 600;
}

label.checkbox {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 500;
}

label.checkbox input {
  width: 18px;
  height: 18px;
}

input,
select {
  width: 100%;
  background: rgba(9, 19, 30, 0.8);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px 14px;
}

input:disabled {
  opacity: 0.6;
}

.form-error {
  margin: 0;
  padding: 10px 12px;
  border-radius: 10px;
  background: rgba(239, 91, 91, 0.12);
  color: #ffc4c4;
  font-size: 0.9rem;
}

.table-wrap {
  overflow-x: auto;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

.data-table th,
.data-table td {
  text-align: left;
  padding: 10px 8px;
  border-bottom: 1px solid var(--line);
  vertical-align: top;
}

.data-table th {
  color: var(--muted);
  font-weight: 600;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.data-table tr.is-disabled td {
  opacity: 0.6;
}

.row-actions {
  white-space: nowrap;
  text-align: right;
}

/* --- Toast ---------------------------------------------------------------- */

.toast {
  position: fixed;
  right: 16px;
  bottom: 16px;
  max-width: calc(100vw - 32px);
  padding: 12px 16px;
  border-radius: 12px;
  background: var(--panel-soft);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  z-index: 40;
}

.toast.success {
  border-color: rgba(37, 209, 155, 0.5);
}

.toast.error {
  border-color: rgba(239, 91, 91, 0.6);
  color: #ffc4c4;
}

/* --- Responsive ----------------------------------------------------------- */

@media (max-width: 1100px) {
  .two-columns,
  .two-columns.wide-left {
    grid-template-columns: 1fr;
  }
}

/* Celular y tablet: barra superior fina con menú desplegable. */
@media (max-width: 1023px) {
  :root {
    --content-pad: 10px;
    --topbar-h: 48px;
  }

  .app-shell {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto 1fr;
    align-content: start;
  }

  .sidebar {
    height: auto;
    padding: 0 12px;
    gap: 0;
    border-right: none;
    border-bottom: 1px solid var(--line);
    background: rgba(7, 18, 30, 0.96);
    backdrop-filter: blur(8px);
  }

  .brand-block {
    height: var(--topbar-h);
    padding: 0;
  }

  .brand-kicker,
  .user-chip {
    display: none;
  }

  .brand-block h1 {
    font-size: 1.2rem;
  }

  .status-dot {
    display: block;
  }

  .icon-button {
    display: grid;
  }

  .fullscreen-button .icon-compress,
  .fullscreen-button.is-active .icon-expand {
    display: none;
  }

  .fullscreen-button.is-active .icon-compress {
    display: block;
  }

  /* El menú se despliega debajo de la barra, sobre el contenido. */
  .sidebar-menu {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    padding: 12px;
    gap: 12px;
    background: #07121e;
    border-bottom: 1px solid var(--line);
    box-shadow: var(--shadow);
    max-height: calc(100vh - var(--topbar-h));
    overflow-y: auto;
  }

  .sidebar.open .sidebar-menu {
    display: flex;
  }

  .sidebar-footer {
    grid-template-columns: 1fr auto;
    align-items: center;
  }

  .status-pill {
    padding: 6px 10px;
    font-size: 0.8rem;
  }

  .form-panel,
  .table-panel {
    padding: 16px;
  }
}

/* Vertical (celular parado): acciones arriba en 2 columnas, cámaras abajo. */
@media (orientation: portrait) and (max-width: 1023px) {
  .dashboard.active {
    grid-template-columns: minmax(0, 1fr);
    gap: 10px;
  }

  .controls-panel {
    padding: 10px;
    gap: 8px;
    border-radius: 16px;
  }

  .device-list,
  .dashboard.no-cameras .device-list {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 6px;
  }

  .device-tile {
    min-height: 46px;
    padding: 6px 10px;
    gap: 8px;
  }

  .device-name {
    font-size: 0.88rem;
    white-space: normal;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }

  .device-state {
    padding: 3px 6px;
    font-size: 0.65rem;
  }
}

/* Celular apaisado (poca altura): todo más compacto. */
@media (orientation: landscape) and (max-height: 540px) {
  :root {
    --topbar-h: 38px;
    --content-pad: 6px;
  }

  .brand-block h1 {
    font-size: 1rem;
  }

  .icon-button {
    width: 32px;
    height: 32px;
    border-radius: 9px;
  }

  .icon-button svg {
    width: 18px;
    height: 18px;
  }

  .dashboard.active {
    grid-template-columns: 180px minmax(0, 1fr);
    gap: 8px;
  }

  .controls-panel {
    padding: 8px;
    gap: 6px;
    border-radius: 14px;
  }

  .controls-header h2 {
    display: none;
  }

  .quick-buttons {
    flex: 1;
  }

  .quick-buttons .action-button {
    flex: 1;
    padding: 6px 8px;
  }

  .device-tile {
    min-height: 40px;
    padding: 5px 10px;
  }

  /* Columna angosta: sin ícono y nombre en hasta 2 líneas para que se lea completo. */
  .device-tile {
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 6px;
  }

  .device-icon {
    display: none;
  }

  .device-name {
    font-size: 0.85rem;
    white-space: normal;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }

  .viewer-bar {
    padding: 4px 10px;
  }
}

@media (max-width: 600px) {
  .inline-fields {
    grid-template-columns: 1fr;
  }

}
