:root {
  --bg: #111418;
  --card: #1c2026;
  --card-2: #23282f;
  --line: #2e343d;
  --text: #e6e9ee;
  --muted: #8d95a3;
  --accent: #38bdf8;
  --on: #ffb84d;
  --off: #4b5563;
  --bad: #f87171;
  --ok: #4ade80;
  --radius: 14px;
}

* { box-sizing: border-box; }

/* El atributo hidden lo pisan las reglas de display de mas abajo (#login es
   grid). Sin esto, login y panel se dibujan uno encima del otro. */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

button { font: inherit; color: inherit; cursor: pointer; }

/* ---------------- login ---------------- */

#login {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

#login form {
  width: min(360px, 100%);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 28px;
}

#login h1 { margin: 0 0 4px; font-size: 22px; }
#login p.sub { margin: 0 0 22px; color: var(--muted); font-size: 13px; }

#login label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }

#login input {
  width: 100%;
  padding: 11px 12px;
  margin-bottom: 16px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 9px;
  color: var(--text);
  font: inherit;
}
#login input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

#login button {
  width: 100%;
  padding: 11px;
  background: var(--accent);
  color: #06212e;
  font-weight: 600;
  border: 0;
  border-radius: 9px;
}
#login button:disabled { opacity: .6; cursor: default; }

.error {
  color: var(--bad);
  font-size: 13px;
  min-height: 18px;
  margin: 0 0 12px;
}

/* ---------------- shell ---------------- */

header {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 12px 18px;
  background: rgba(17, 20, 24, .88);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
}

header .brand { font-weight: 600; margin-right: 14px; }

nav button {
  background: none;
  border: 0;
  padding: 7px 13px;
  border-radius: 8px;
  color: var(--muted);
  font-size: 14px;
}
nav button.active { background: var(--card-2); color: var(--text); }

header .spacer { flex: 1; }

#conn {
  font-size: 12px;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 6px;
}
#conn .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--off); }
#conn.live .dot { background: var(--ok); }
#conn.down .dot { background: var(--bad); }

#logout { background: none; border: 1px solid var(--line); border-radius: 8px; padding: 6px 11px; font-size: 13px; color: var(--muted); }

/* ---------------- grid de tarjetas ---------------- */

main { padding: 18px; max-width: 1400px; margin: 0 auto; }

.view { display: none; }
.view.active { display: block; }

.masonry {
  /* Masonry sobre grid con filas finas: cada tarjeta ocupa las filas que
     necesita segun su altura real, calculada en JS. Es la unica forma de tener
     a la vez relleno sin huecos Y una tarjeta que cruce todas las columnas:
     con multi-columna, `column-span: all` parte el flujo en dos bloques y deja
     columnas vacias; con grid normal, cada fila toma la altura de su tarjeta
     mas alta y quedan huecos enormes. */
  display: grid;
  grid-template-columns: repeat(var(--cols, 3), minmax(0, 1fr));
  grid-auto-rows: var(--fila, 8px);
  column-gap: var(--hueco, 16px);
  row-gap: 0;
  align-items: start;
}

.card { grid-row-end: span var(--span, 20); }

/* Una tarjeta a todo lo ancho SIEMPRE rompe el masonry: obliga a que todas las
   columnas cierren su fila antes de colocarla, y deja huecos de cientos de
   pixeles. Por eso el grafico se trata como una tarjeta normal; a ancho de
   columna se lee igual de bien. */
.card.wide { grid-column: auto; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  break-inside: avoid;
}
.card > h2 {
  margin: 0 0 12px;
  font-size: 15px;
  font-weight: 600;
}

/* ---------------- filas de entidades ---------------- */

.row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 9px 0;
  border-bottom: 1px solid var(--line);
}
.row:last-child { border-bottom: 0; }
.row .icon { width: 22px; text-align: center; font-size: 17px; color: var(--muted); }
.row.on .icon { color: var(--on); }
.row .label { flex: 1; min-width: 0; }
.row .label b { display: block; font-weight: 500; }
.row .label small { color: var(--muted); font-size: 12px; }
.row .label small.warn { color: var(--bad); }

/* interruptor */
.toggle {
  width: 44px; height: 25px;
  border-radius: 13px;
  background: var(--off);
  border: 0;
  position: relative;
  flex: none;
  transition: background .15s;
}
.toggle::after {
  content: "";
  position: absolute; top: 3px; left: 3px;
  width: 19px; height: 19px;
  border-radius: 50%;
  background: #fff;
  transition: transform .15s;
}
.toggle.on { background: var(--on); }
.toggle.on::after { transform: translateX(19px); }
.toggle:disabled { opacity: .35; cursor: not-allowed; }

/* ---------------- botones cuadrados ---------------- */

.buttons { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 10px; }

.btn-tile {
  background: var(--card-2);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 14px 8px;
  display: grid;
  gap: 7px;
  justify-items: center;
  transition: background .15s, border-color .15s;
}
.btn-tile .icon { font-size: 24px; color: var(--muted); line-height: 1; }
.btn-tile span { font-size: 12px; color: var(--muted); }
.btn-tile.on { background: rgba(255, 184, 77, .13); border-color: rgba(255, 184, 77, .4); }
.btn-tile.on .icon, .btn-tile.on span { color: var(--on); }
.btn-tile:disabled { opacity: .4; cursor: not-allowed; }

/* ---------------- clima ---------------- */

.wx-now { display: flex; align-items: center; gap: 16px; margin-bottom: 14px; }
.wx-now .big { font-size: 42px; font-weight: 300; line-height: 1; }
.wx-now .glyph { font-size: 40px; }
.wx-now .meta { color: var(--muted); font-size: 13px; }
.wx-now .meta b { color: var(--text); font-weight: 500; text-transform: capitalize; display: block; font-size: 14px; }

.wx-days { display: grid; grid-template-columns: repeat(5, 1fr); gap: 4px; text-align: center; border-top: 1px solid var(--line); padding-top: 12px; }
.wx-days div small { display: block; color: var(--muted); font-size: 11px; }
.wx-days div .g { font-size: 19px; margin: 3px 0; }
.wx-days div .t { font-size: 12px; }
.wx-days div .t i { color: var(--muted); font-style: normal; }

/* ---------------- termostato ---------------- */

.thermo { text-align: center; }
.thermo .dial {
  width: 168px; height: 168px;
  margin: 4px auto 12px;
  border-radius: 50%;
  border: 9px solid var(--card-2);
  display: grid; place-content: center;
}
.thermo .dial b { font-size: 34px; font-weight: 300; }
.thermo .dial small { color: var(--muted); font-size: 12px; }
.thermo .modes { display: flex; justify-content: center; gap: 8px; }
.thermo .modes button {
  width: 38px; height: 38px;
  border-radius: 50%;
  background: var(--card-2);
  border: 1px solid var(--line);
  color: var(--muted);
}
.thermo .modes button.active { background: var(--accent); color: #06212e; border-color: transparent; }

/* ---------------- control remoto ---------------- */

.remote { display: grid; gap: 10px; justify-items: center; }
.remote .pad {
  display: grid;
  grid-template-columns: repeat(3, 52px);
  grid-template-rows: repeat(3, 52px);
  gap: 6px;
}
.remote .pad button, .remote .strip button {
  background: var(--card-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  color: var(--muted);
  font-size: 15px;
}
.remote .pad button:active, .remote .strip button:active { background: var(--accent); color: #06212e; }
.remote .pad .ok { border-radius: 50%; background: var(--card); color: var(--text); font-size: 12px; }
.remote .strip { display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; }
.remote .strip button { padding: 8px 13px; }
.remote .strip button.pow { color: var(--bad); }

/* ---------------- grafico ---------------- */

.chart-head { display: flex; gap: 18px; margin-bottom: 10px; font-size: 13px; }
.chart-head b { font-size: 19px; font-weight: 500; display: block; }
.chart-head .dn b { color: var(--accent); }
.chart-head .up b { color: #a78bfa; }
.chart-head span { color: var(--muted); font-size: 12px; }
canvas { width: 100%; height: 150px; display: block; }

/* ---------------- cerradura ---------------- */

.lock-btn {
  width: 100%;
  padding: 13px;
  border-radius: 11px;
  border: 1px solid var(--line);
  background: var(--card-2);
  margin-bottom: 12px;
  font-weight: 500;
}
.lock-btn.unlocked { background: rgba(74, 222, 128, .13); border-color: rgba(74, 222, 128, .4); color: var(--ok); }
.lock-btn:disabled { opacity: .4; cursor: not-allowed; }

/* ---------------- avisos ---------------- */

.notice {
  background: rgba(248, 113, 113, .1);
  border: 1px solid rgba(248, 113, 113, .3);
  color: #fca5a5;
  border-radius: 10px;
  padding: 10px 13px;
  font-size: 13px;
  margin-bottom: 16px;
}
.notice b { color: #fecaca; }

#toast {
  position: fixed;
  left: 50%; bottom: 26px;
  transform: translate(-50%, 80px);
  background: var(--card-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 11px 16px;
  font-size: 13px;
  max-width: 90vw;
  opacity: 0;
  transition: opacity .2s, transform .2s;
  pointer-events: none;
  z-index: 20;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.bad { border-color: rgba(248, 113, 113, .5); color: #fca5a5; }

/* ---------------- escala por tamano de pantalla ---------------- */

/* Telefono: una columna. */
@media (max-width: 559px) {
  main { padding: 12px; }
  .masonry { --cols: 1; --hueco: 12px; }
  header { padding: 10px 12px; gap: 4px; }
  header .brand { margin-right: 8px; }
  nav button { padding: 7px 10px; font-size: 13px; }
  #conn .txt { display: none; }
}

/* Tablet de 7" en vertical (600-800 px): dos columnas justas. */
@media (min-width: 560px) and (max-width: 899px) {
  main { padding: 14px; }
  .masonry { --cols: 2; --hueco: 14px; }
  .card { padding: 14px; }
  .buttons { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  canvas { height: 130px; }
}

/* Tablet grande o 7" en horizontal. */
@media (min-width: 900px) and (max-width: 1199px) {
  .masonry { --cols: 3; }
}

@media (min-width: 1200px) {
  .masonry { --cols: 4; }
}

/* Pantalla baja en horizontal: recuperar altura vertical. */
@media (max-height: 560px) and (orientation: landscape) {
  header { position: static; }
  .thermo .dial { width: 130px; height: 130px; border-width: 7px; }
  .thermo .dial b { font-size: 27px; }
  canvas { height: 110px; }
}

/* ---------------- dedo en vez de raton ---------------- */

@media (pointer: coarse) {
  /* Objetivos de toque de al menos 44 px, que es el minimo comodo. */
  .toggle { width: 54px; height: 31px; }
  .toggle::after { width: 25px; height: 25px; }
  .toggle.on::after { transform: translateX(23px); }
  .row { padding: 12px 0; }
  .btn-tile { padding: 18px 8px; }
  .remote .pad { grid-template-columns: repeat(3, 62px); grid-template-rows: repeat(3, 62px); }
  .remote .strip button { padding: 12px 16px; font-size: 17px; }
  .mini { padding: 9px 14px; font-size: 13px; }
  nav button { padding: 10px 14px; }
  .dev-row { padding: 12px; gap: 8px; }
  /* El arrastre por toque lo maneja el JS; el nativo interferiria con el scroll. */
  .card[draggable="true"] { cursor: default; }
}

/* Menos ruido de animacion para quien lo pida. */
@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

dialog.modal { width: min(560px, 94vw); }
@media (max-width: 559px) {
  dialog.modal { width: 100vw; max-width: 100vw; height: 100dvh; max-height: 100dvh; border-radius: 0; }
}

/* ---------------- vista de aparatos ---------------- */

.dev-driver { margin-bottom: 22px; }
.dev-driver > h2 {
  font-size: 14px; font-weight: 600; margin: 0 0 10px;
  display: flex; align-items: center; gap: 10px;
}
.dev-driver > h2 .count { color: var(--muted); font-weight: 400; font-size: 12px; }
.dev-driver > h2 .spacer { flex: 1; }

.mini {
  background: var(--card-2); border: 1px solid var(--line); color: var(--muted);
  border-radius: 8px; padding: 5px 11px; font-size: 12px;
}
.mini:hover { color: var(--text); }
.mini.primary { background: var(--accent); color: #06212e; border-color: transparent; font-weight: 600; }
.mini.danger { color: var(--bad); }

.dev-table { display: grid; gap: 6px; }

.dev-row {
  display: flex; align-items: center; gap: 10px;
  background: var(--card); border: 1px solid var(--line);
  border-radius: 10px; padding: 9px 12px;
}
.dev-row .grow { flex: 1; min-width: 0; }
.dev-row b { font-weight: 500; display: block; font-size: 14px; }
.dev-row small { color: var(--muted); font-size: 12px; word-break: break-all; }
.dev-row .tag {
  font-size: 11px; padding: 2px 7px; border-radius: 6px;
  background: var(--card-2); color: var(--muted); flex: none;
}
.dev-row .tag.warn { background: rgba(248,113,113,.14); color: #fca5a5; }

.editor { display: grid; gap: 12px; margin-top: 10px; }
.editor .field { display: grid; gap: 5px; }
.editor label { font-size: 12px; color: var(--muted); }
.editor input, .editor select, .editor textarea {
  background: var(--bg); border: 1px solid var(--line); color: var(--text);
  border-radius: 8px; padding: 8px 10px; font: inherit; width: 100%;
}
.editor textarea { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 11px; min-height: 68px; resize: vertical; }
.editor input:focus, .editor select:focus, .editor textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.editor .actions { display: flex; gap: 8px; justify-content: flex-end; }

dialog.modal {
  background: var(--card); color: var(--text);
  border: 1px solid var(--line); border-radius: var(--radius);
  padding: 20px; width: min(520px, 94vw); max-height: 86vh; overflow: auto;
}
dialog.modal::backdrop { background: rgba(0,0,0,.6); }
dialog.modal h3 { margin: 0 0 4px; font-size: 16px; }
dialog.modal p.hint { margin: 0 0 14px; color: var(--muted); font-size: 12px; }

/* ---------------- arrastrar tarjetas ---------------- */

.card[draggable="true"] { cursor: grab; }
.card.dragging {
  opacity: .45;
  cursor: grabbing;
  outline: 2px dashed var(--accent);
  outline-offset: 2px;
}
/* Los controles no deben iniciar un arrastre al tocarlos. */
.card button, .card input, .card select, .card canvas { -webkit-user-drag: none; }

/* ---------------- estado de carga ---------------- */

.row .label small.cargando,
.thermo .dial small.cargando { color: var(--muted); opacity: .75; }

@keyframes latido { 0%,100% { opacity: .35 } 50% { opacity: .7 } }
.btn-tile.cargando { animation: latido 1.4s ease-in-out infinite; }
.btn-tile.cargando:disabled { opacity: 1; }

/* ---------------- arrastre por toque ---------------- */

.card.touch-drag {
  position: relative;
  z-index: 30;
  opacity: .9;
  transform: scale(1.02);
  box-shadow: 0 12px 32px rgba(0, 0, 0, .5);
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.masonry.reordenando { touch-action: none; }
.drag-hint {
  position: fixed; left: 50%; bottom: 22px; transform: translateX(-50%);
  background: var(--accent); color: #06212e; font-size: 13px; font-weight: 600;
  padding: 9px 16px; border-radius: 10px; z-index: 40; pointer-events: none;
}

/* ---------------- accesos a apps del televisor ---------------- */

.remote .apps {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(88px, 1fr));
  gap: 6px;
  width: 100%;
  margin-top: 4px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
}
.app-btn {
  display: grid;
  justify-items: center;
  gap: 4px;
  padding: 10px 6px;
  background: var(--card-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  color: var(--muted);
  font-size: 12px;
}
.app-btn .ico { font-size: 20px; line-height: 1; }
.app-btn:hover:not(:disabled) { color: var(--text); border-color: var(--accent); }
.app-btn:disabled { opacity: .5; cursor: wait; }
.app-btn.wake { color: var(--muted); opacity: .8; }

@media (pointer: coarse) { .app-btn { padding: 14px 6px; } }

/* El cerrojo tarda unos segundos por Bluetooth: hay que verlo trabajar. */
.lock-btn.ocupado {
  animation: latido 1.2s ease-in-out infinite;
  cursor: wait;
}

/* Distintivo del modo sin conexion. */
.sin-nube {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .04em;
  text-transform: uppercase;
  color: var(--ok);
  background: rgba(74, 222, 128, .13);
  border: 1px solid rgba(74, 222, 128, .35);
  border-radius: 6px;
  padding: 3px 8px;
  margin-right: 10px;
}

/* ---------------- interruptor de modo sin conexion ---------------- */

#offline-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  background: none;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px 11px;
  margin-right: 10px;
  font-size: 12px;
  color: var(--muted);
}
#offline-toggle .dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--off);
}
#offline-toggle:hover { color: var(--text); }
#offline-toggle.activo {
  color: var(--ok);
  border-color: rgba(74, 222, 128, .4);
  background: rgba(74, 222, 128, .1);
}
#offline-toggle.activo .dot { background: var(--ok); }

#offline-toggle .txt-corto { display: none; }
@media (max-width: 559px) {
  #offline-toggle .txt { display: none; }
  #offline-toggle .txt-corto { display: inline; }
  #offline-toggle { padding: 6px 9px; margin-right: 6px; }
}
@media (pointer: coarse) { #offline-toggle { padding: 9px 12px; } }
