:root {
  color-scheme: light dark;
  --bg: #f5f6f8;
  --superficie: #ffffff;
  --texto: #1a1d21;
  --texto-tenue: #6b7280;
  --borde: #e2e4e8;
  --acento: #2563eb;
  --acento-texto: #ffffff;
  --exito: #16a34a;
  --error: #dc2626;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15171a;
    --superficie: #1f2227;
    --texto: #e9eaec;
    --texto-tenue: #9aa0a8;
    --borde: #30343a;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--texto);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 1.25rem;
  padding-top: calc(1rem + env(safe-area-inset-top, 0px));
  border-bottom: 1px solid var(--borde);
}

header h1 { font-size: 1.1rem; margin: 0; }

main { max-width: 900px; margin: 0 auto; padding: 1.25rem; }

#vista-login {
  max-width: 320px;
  margin: 3rem auto;
}

#form-login {
  background: var(--superficie);
  border: 1px solid var(--borde);
  border-radius: 12px;
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}

label {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.85rem;
  color: var(--texto-tenue);
}

input {
  padding: 0.6rem 0.7rem;
  border-radius: 8px;
  border: 1px solid var(--borde);
  background: var(--bg);
  color: var(--texto);
  font-size: 1rem;
}

button {
  padding: 0.6rem 1rem;
  border-radius: 8px;
  border: none;
  background: var(--acento);
  color: var(--acento-texto);
  font-size: 0.95rem;
  cursor: pointer;
}

button:hover { opacity: 0.9; }

#btn-logout, #btn-refrescar {
  background: transparent;
  color: var(--texto-tenue);
  border: 1px solid var(--borde);
}

.error { color: var(--error); font-size: 0.85rem; }
.hint { color: var(--texto-tenue); font-size: 0.9rem; }

.barra-estado {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
  font-size: 0.85rem;
  color: var(--texto-tenue);
}

.grilla {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 0.9rem;
}

.tarjeta-dispositivo {
  background: var(--superficie);
  border: 1px solid var(--borde);
  border-radius: 12px;
  padding: 1rem;
  text-align: left;
  width: 100%;
  cursor: pointer;
  font: inherit;
  color: inherit;
  display: block;
}

.tarjeta-dispositivo:hover {
  border-color: var(--acento);
  opacity: 1;
}

.tarjeta-dispositivo h3 {
  margin: 0 0 0.3rem 0;
  font-size: 1rem;
}

.tarjeta-dispositivo .categoria {
  color: var(--texto-tenue);
  font-size: 0.8rem;
  margin-bottom: 0.6rem;
}

.punto-estado {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 0.4rem;
}
.punto-estado.online { background: var(--exito); }
.punto-estado.offline { background: var(--texto-tenue); }

/* --- Grupos de categorías --- */
.grupo-categoria {
  margin-bottom: 1.75rem;
}

.grupo-categoria h2 {
  font-size: 0.95rem;
  color: var(--texto-tenue);
  margin: 0 0 0.6rem 0;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

/* --- Modal --- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  align-items: flex-end;
  justify-content: center;
  z-index: 100;
  /* display NO se fija acá a propósito: si esta regla pusiera
     "display: flex" sin condición, le ganaría en especificidad al
     "display: none" que el navegador aplica por el atributo [hidden]
     (misma especificidad, pero esta regla de autor va después en la
     cascada) — y el overlay quedaría SIEMPRE visible e interceptando
     clics en toda la página, aunque tenga hidden. Bug real encontrado
     en producción: bloqueaba toda la página desde el primer clic. */
}

.modal-overlay:not([hidden]) {
  display: flex;
}

@media (min-width: 640px) {
  .modal-overlay { align-items: center; }
}

.modal {
  background: var(--superficie);
  width: 100%;
  max-width: 480px;
  max-height: 85vh;
  overflow-y: auto;
  border-radius: 16px 16px 0 0;
  padding: 1.25rem;
  padding-bottom: calc(1.25rem + env(safe-area-inset-bottom, 0px));
}

@media (min-width: 640px) {
  .modal { border-radius: 16px; }
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.5rem;
}

.modal-header h2 { margin: 0; font-size: 1.1rem; }

.btn-cerrar {
  background: transparent;
  color: var(--texto-tenue);
  border: none;
  font-size: 1.1rem;
  padding: 0.3rem 0.6rem;
}

.modal-seccion {
  margin-top: 1rem;
}

.modal-seccion:empty { display: none; }

/* --- Toggle de interruptor --- */
.fila-switch {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.6rem 0;
  border-bottom: 1px solid var(--borde);
}
.fila-switch:last-child { border-bottom: none; }

.toggle {
  position: relative;
  width: 46px;
  height: 26px;
  flex-shrink: 0;
}

.toggle input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-slider {
  position: absolute;
  inset: 0;
  background: var(--borde);
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.15s ease;
}

.toggle-slider::before {
  content: "";
  position: absolute;
  width: 20px;
  height: 20px;
  left: 3px;
  top: 3px;
  background: white;
  border-radius: 50%;
  transition: transform 0.15s ease;
}

.toggle input:checked + .toggle-slider {
  background: var(--exito);
}

.toggle input:checked + .toggle-slider::before {
  transform: translateX(20px);
}

.toggle input:disabled + .toggle-slider {
  opacity: 0.5;
  cursor: wait;
}

/* --- Tarjetas de energía --- */
.grilla-energia {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.6rem;
}

.stat-energia {
  background: var(--bg);
  border-radius: 10px;
  padding: 0.7rem;
}

.stat-energia .valor {
  font-size: 1.3rem;
  font-weight: 600;
}

.stat-energia .etiqueta {
  font-size: 0.75rem;
  color: var(--texto-tenue);
}

.canal-energia {
  margin-bottom: 0.9rem;
}

.canal-energia h4 {
  font-size: 0.8rem;
  color: var(--texto-tenue);
  margin: 0 0 0.4rem 0;
  text-transform: uppercase;
}

/* --- Panel de batería solar --- */
.bateria-soc {
  text-align: center;
  padding: 0.75rem 0 1rem 0;
}

.bateria-soc .soc-valor {
  font-size: 2.2rem;
  font-weight: 700;
}

.bateria-soc .etiqueta {
  font-size: 0.8rem;
  color: var(--texto-tenue);
}

.celdas-bateria {
  margin-top: 0.75rem;
  font-size: 0.85rem;
}

.celdas-bateria summary {
  cursor: pointer;
  color: var(--texto-tenue);
}

.grilla-celdas {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.4rem;
  margin-top: 0.6rem;
}

.celda {
  background: var(--bg);
  border-radius: 8px;
  padding: 0.4rem;
  text-align: center;
  font-size: 0.78rem;
}

.celda span {
  display: block;
  color: var(--texto-tenue);
  font-size: 0.7rem;
}

/* --- Detalle crudo (fallback) --- */
.tabla-detalle {
  width: 100%;
  font-size: 0.85rem;
  border-collapse: collapse;
}

.tabla-detalle td {
  padding: 0.3rem 0;
  border-bottom: 1px solid var(--borde);
}

.tabla-detalle td:first-child {
  color: var(--texto-tenue);
  padding-right: 0.75rem;
  white-space: nowrap;
}

/* --- Navegación por pestañas --- */
.tabs {
  display: flex;
  gap: 0.4rem;
  overflow-x: auto;
  margin-bottom: 1rem;
  -webkit-overflow-scrolling: touch;
}

.tab {
  background: var(--superficie);
  color: var(--texto-tenue);
  border: 1px solid var(--borde);
  white-space: nowrap;
  font-size: 0.85rem;
  padding: 0.5rem 0.9rem;
}

.tab.activa {
  background: var(--acento);
  color: var(--acento-texto);
  border-color: var(--acento);
}

.pantalla:not([hidden]) {
  display: block;
}

.boton-secundario {
  background: transparent;
  color: var(--acento);
  border: 1px dashed var(--acento);
  width: 100%;
  margin-top: 0.75rem;
}

/* --- Pantalla de interruptores --- */
.barra-filtro {
  margin-bottom: 0.9rem;
  font-size: 0.85rem;
  color: var(--texto-tenue);
}

.barra-filtro select {
  margin-left: 0.4rem;
  padding: 0.3rem 0.5rem;
  border-radius: 6px;
  border: 1px solid var(--borde);
  background: var(--superficie);
  color: var(--texto);
}

.grupo-interruptores h2 {
  font-size: 0.95rem;
  color: var(--texto-tenue);
  margin: 1.2rem 0 0.6rem 0;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.fila-interruptor {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--superficie);
  border: 1px solid var(--borde);
  border-radius: 10px;
  padding: 0.7rem 0.9rem;
  margin-bottom: 0.5rem;
  gap: 0.75rem;
}

.fila-interruptor .info {
  min-width: 0;
  flex: 1;
}

.fila-interruptor .nombre {
  font-weight: 600;
  font-size: 0.95rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.fila-interruptor .detalle {
  font-size: 0.78rem;
  color: var(--texto-tenue);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.fila-interruptor .acciones {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-shrink: 0;
}

.btn-editar {
  background: transparent;
  border: 1px solid var(--borde);
  color: var(--texto-tenue);
  font-size: 0.8rem;
  padding: 0.35rem 0.6rem;
}

.badge-espacio {
  display: inline-block;
  background: var(--bg);
  border-radius: 999px;
  padding: 0.1rem 0.5rem;
  font-size: 0.72rem;
  color: var(--texto-tenue);
  margin-top: 0.2rem;
}

/* --- Pantalla de espacios --- */
.tarjeta-espacio {
  background: var(--superficie);
  border: 1px solid var(--borde);
  border-radius: 12px;
  padding: 1.1rem;
  text-align: center;
  cursor: pointer;
  font: inherit;
  color: inherit;
}

.tarjeta-espacio:hover { border-color: var(--acento); }

.tarjeta-espacio .icono { font-size: 1.8rem; display: block; margin-bottom: 0.3rem; }
.tarjeta-espacio .nombre { font-weight: 600; font-size: 0.9rem; }
.tarjeta-espacio .cantidad { font-size: 0.75rem; color: var(--texto-tenue); }

#espacio-detalle {
  margin-top: 1.25rem;
}

#espacio-detalle h3 {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.link-tenue {
  background: transparent;
  color: var(--texto-tenue);
  border: none;
  font-size: 0.8rem;
  text-decoration: underline;
  padding: 0;
}

/* --- Pantalla de energía --- */
.bloque-energia-pantalla {
  background: var(--superficie);
  border: 1px solid var(--borde);
  border-radius: 12px;
  padding: 1rem;
  margin-bottom: 1rem;
}

.bloque-energia-pantalla h3 {
  margin: 0 0 0.7rem 0;
  font-size: 1rem;
}

.sparkline {
  display: block;
  margin-top: 0.5rem;
}

/* --- Modal chico (editar interruptor) --- */
.modal-chico {
  max-width: 360px;
}

#form-editar-interruptor {
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  margin-top: 0.5rem;
}

#form-editar-interruptor select {
  padding: 0.6rem 0.7rem;
  border-radius: 8px;
  border: 1px solid var(--borde);
  background: var(--bg);
  color: var(--texto);
  font-size: 1rem;
}
