/* Reset */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

/* Variables de color — modo oscuro por defecto */
:root {
  --bg:        #1C1C1A;
  --bg-card:   #242422;
  --bg-sub:    #2C2C2A;
  --border:    rgba(255,255,255,0.10);
  --border-md: rgba(255,255,255,0.18);
  --text:      #E8E6DF;
  --text-sec:  #B4B2A9;
  --text-ter:  #888780;
  --green:     #1D9E75;
  --green-bg:  #0A2A1A;
  --green-txt: #5DCAA5;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
}

/* Modo claro — solo si el sistema lo soporta y lo pide */
@media (prefers-color-scheme: light) {
  :root {
    --bg:        #F4F4F0;
    --bg-card:   #FFFFFF;
    --bg-sub:    #F1EFE8;
    --border:    rgba(0,0,0,0.10);
    --border-md: rgba(0,0,0,0.18);
    --text:      #1a1a18;
    --text-sec:  #5F5E5A;
    --text-ter:  #888780;
    --green-bg:  #EAF3DE;
    --green-txt: #27500A;
  }
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.5;
  min-height: 100vh;
}

/* App shell — mobile first */
.app {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

/* Navbar — mobile: fija abajo */
.navbar {
  background: var(--bg-card);
  border-top: 0.5px solid var(--border);
  padding: 8px 0 calc(8px + env(safe-area-inset-bottom));
  display: flex;
  justify-content: space-around;
  position: fixed;
  bottom: 0; left: 0; right: 0;
  max-width: 480px;
  margin: 0 auto;
  z-index: 10;
}
.nav-item {
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  font-size: 11px; color: var(--text-ter); cursor: pointer;
  padding: 4px 14px; border-radius: var(--radius-sm); text-decoration: none;
  transition: color .15s;
}
.nav-item.active { color: var(--green); }
.nav-item svg { width: 20px; height: 20px; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* ════════════════════════════════════════
   DESKTOP (≥ 768px) — layout expandido
   ════════════════════════════════════════ */
@media (min-width: 768px) {

  /* Expandir el shell a todo el ancho */
  .app,
  .app.app-with-sidebar {
    max-width: 1200px !important;
    width: 100%;
    flex-direction: row !important;
  }

  /* Navbar: de bottom bar a sidebar izquierda */
  .navbar {
    position: sticky !important;
    top: 0 !important;
    bottom: auto !important;
    left: auto !important;
    right: auto !important;
    max-width: 200px !important;
    min-width: 200px !important;
    width: 200px !important;
    height: 100vh !important;
    flex-direction: column !important;
    justify-content: flex-start !important;
    align-items: stretch !important;
    border-top: none !important;
    border-right: 0.5px solid var(--border);
    padding: 20px 0 !important;
    margin: 0 !important;
    gap: 2px;
    flex-shrink: 0;
    z-index: 10;
  }

  .nav-item {
    flex-direction: row !important;
    justify-content: flex-start !important;
    align-items: center;
    padding: 11px 20px !important;
    font-size: 14px !important;
    gap: 12px !important;
    border-radius: 0 !important;
    color: var(--text-sec);
    width: 100%;
  }
  .nav-item:hover { background: var(--bg-sub); color: var(--text); }
  .nav-item.active {
    background: var(--green-bg) !important;
    color: var(--green) !important;
    font-weight: 500;
  }
  .nav-item svg { width: 18px !important; height: 18px !important; flex-shrink: 0; }

  /* Área de contenido al lado del sidebar */
  .app-main {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    overflow-y: auto;
  }

  /* Topbar y contenido */
  .topbar { padding: 16px 28px; }
  .content { padding: 20px 28px 40px; }

  /* Sensores: una fila de 5 cards (3 + DPV ocupa 2 = 5 slots) */
  .sensors-grid {
    grid-template-columns: repeat(5, minmax(0, 1fr)) !important;
    gap: 12px;
  }
  /* En desktop el DPV no necesita span 2, ocupa 1 slot normal */
  .sensor-card.wide {
    grid-column: span 2 !important;
  }
  .sensor-val  { font-size: 26px; }
  .dpv-number  { font-size: 28px; }
  .sensor-card { padding: 14px 10px; }

  /* Dashboard inferior: devices izq, gráfica der */
  .dashboard-layout {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 24px;
    align-items: start;
  }
  .dashboard-col { min-width: 0; }

  /* Devices: SIEMPRE 2 columnas en desktop — nunca 4 */
  .devices-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 8px;
  }

  /* Device card: texto no se corta, toggle no se superpone */
  .device-card {
    padding: 12px 10px;
    gap: 8px;
    min-width: 0;
  }
  .dev-name {
    font-size: 13px;
    white-space: normal !important;      /* permite wrap del nombre */
    overflow: visible !important;
    text-overflow: clip !important;
    line-height: 1.3;
  }
  .dev-status-on, .dev-status-off { font-size: 11px; }
  .dev-icon { width: 32px; height: 32px; flex-shrink: 0; }
  .toggle   { flex-shrink: 0; }

  /* Gráfica mini */
  .chart-wrap { height: 220px; }

  /* desktop-only: visible solo en desktop */
  .desktop-only { display: block; }

  /* Gráfica más alta */
  .chart-wrap { height: 220px; }

  /* Period row sin overflow */
  .period-row { flex-wrap: wrap; overflow: visible; }
}

/* Ocultar columna gráfica en mobile */
.desktop-only { display: none; }

@media (min-width: 1100px) {
  .navbar { max-width: 220px !important; min-width: 220px !important; width: 220px !important; }
  .sensor-val { font-size: 30px; }
}

/* ════════════════════════════════════════ */


.topbar {
  background: var(--bg-card);
  border-bottom: 0.5px solid var(--border);
  padding: 14px 16px;
  display: flex;
  align-items: center;
  gap: 10px;
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar-title { font-size: 15px; font-weight: 500; flex: 1; }
.topbar-id { font-size: 12px; color: var(--text-ter); }

.back-btn {
  width: 32px; height: 32px;
  border-radius: var(--radius-sm);
  border: 0.5px solid var(--border);
  background: var(--bg-sub);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; flex-shrink: 0; text-decoration: none;
}
.back-btn svg { width: 16px; height: 16px; stroke: var(--text-sec); fill: none; stroke-width: 2; stroke-linecap: round; }

.live-badge {
  display: flex; align-items: center; gap: 5px;
  font-size: 12px; color: var(--green-txt);
  background: var(--green-bg); padding: 4px 10px; border-radius: 20px;
}
.live-dot {
  width: 7px; height: 7px; background: var(--green); border-radius: 50%;
  animation: pulse 2s infinite;
}
@keyframes pulse { 0%,100%{opacity:1} 50%{opacity:.3} }

/* Contenido principal */
.content { padding: 14px 14px 90px; flex: 1; }

/* Cards base */
.card {
  background: var(--bg-card);
  border: 0.5px solid var(--border);
  border-radius: var(--radius-md);
  padding: 14px;
  margin-bottom: 10px;
}

/* Grid de sensores */
.sensors-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  margin-bottom: 10px;
}
.sensor-card {
  background: var(--bg-card);
  border: 0.5px solid var(--border);
  border-radius: var(--radius-md);
  padding: 12px 8px;
  text-align: center;
}
.sensor-card.wide { grid-column: span 2; text-align: left; padding: 12px 14px; }
.sensor-icon { height: 22px; margin-bottom: 5px; display: flex; align-items: center; justify-content: center; }
.sensor-icon svg { width: 20px; height: 20px; fill: none; stroke-width: 2; stroke-linecap: round; }
.sensor-val { font-size: 22px; font-weight: 500; line-height: 1; }
.sensor-unit { font-size: 11px; color: var(--text-sec); margin-top: 2px; }
.sensor-lbl { font-size: 11px; color: var(--text-ter); margin-top: 4px; }

/* DPV bar */
.dpv-row { display: flex; align-items: center; gap: 14px; }
.dpv-val-wrap { flex-shrink: 0; }
.dpv-number { font-size: 26px; font-weight: 500; }
.dpv-unit { font-size: 12px; color: var(--text-sec); }
.dpv-lbl { font-size: 11px; color: var(--text-ter); margin-top: 2px; }
.dpv-bar-wrap { flex: 1; }
.dpv-track { height: 6px; background: var(--bg-sub); border-radius: 3px; overflow: hidden; }
.dpv-fill { height: 100%; background: var(--green); border-radius: 3px; transition: width .5s; }
.dpv-range { display: flex; justify-content: space-between; font-size: 10px; color: var(--text-ter); margin-top: 3px; }

/* Última actualización */
.update-row {
  text-align: center; font-size: 12px; color: var(--text-ter);
  margin-bottom: 12px;
}

/* Sección título */
.section-title {
  font-size: 13px; font-weight: 500; color: var(--text-sec);
  margin-bottom: 8px; margin-top: 4px;
}

/* Grid de dispositivos */
.devices-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}
.device-card {
  background: var(--bg-card);
  border: 0.5px solid var(--border);
  border-radius: var(--radius-md);
  padding: 12px;
  display: flex; align-items: center; gap: 10px;
  cursor: pointer; transition: background .15s;
}
.device-card:hover { background: var(--bg-sub); }
.device-card.on { border-color: var(--green); }
.dev-icon {
  width: 36px; height: 36px; border-radius: var(--radius-sm);
  display: flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.dev-icon svg { width: 18px; height: 18px; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.device-card.on .dev-icon { background: var(--green-bg); }
.device-card.on .dev-icon svg { stroke: var(--green); }
.device-card:not(.on) .dev-icon { background: var(--bg-sub); }
.device-card:not(.on) .dev-icon svg { stroke: var(--text-ter); }
.dev-info { flex: 1; min-width: 0; }
.dev-name { font-size: 13px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dev-status-on  { font-size: 11px; color: var(--green-txt); margin-top: 1px; }
.dev-status-off { font-size: 11px; color: var(--text-ter); margin-top: 1px; }
.toggle {
  width: 36px; height: 20px; background: var(--border-md);
  border-radius: 10px; position: relative; flex-shrink: 0;
  transition: background .2s;
}
.toggle::after {
  content: ''; position: absolute;
  width: 16px; height: 16px; background: white; border-radius: 50%;
  top: 2px; left: 2px; transition: transform .2s;
}
.device-card.on .toggle { background: var(--green); }
.device-card.on .toggle::after { transform: translateX(16px); }

/* ── Estadísticas ── */
.period-row {
  display: flex; gap: 6px; margin-bottom: 12px;
  overflow-x: auto; padding-bottom: 2px; -ms-overflow-style: none; scrollbar-width: none;
}
.period-row::-webkit-scrollbar { display: none; }
.period-btn {
  flex-shrink: 0; padding: 6px 14px; border-radius: 20px;
  border: 0.5px solid var(--border); background: var(--bg-card);
  font-size: 12px; font-weight: 500; cursor: pointer;
  color: var(--text-sec); transition: all .15s;
}
.period-btn.active { background: var(--green); border-color: var(--green); color: #fff; }

.metric-tabs {
  display: flex; background: var(--bg-sub); border-radius: var(--radius-sm);
  padding: 3px; margin-bottom: 12px; gap: 0;
}
.metric-tab {
  flex: 1; padding: 7px 4px; text-align: center;
  border-radius: var(--radius-sm); font-size: 12px; font-weight: 500;
  cursor: pointer; color: var(--text-sec); transition: all .15s; white-space: nowrap;
}
.metric-tab.active {
  background: var(--bg-card); color: var(--text);
  border: 0.5px solid var(--border);
}

.stats-row { display: grid; grid-template-columns: repeat(3, minmax(0,1fr)); gap: 8px; margin-bottom: 12px; }
.stat-card { background: var(--bg-card); border: 0.5px solid var(--border); border-radius: var(--radius-md); padding: 10px; text-align: center; }
.stat-lbl { font-size: 11px; color: var(--text-ter); margin-bottom: 4px; }
.stat-val { font-size: 17px; font-weight: 500; }
.stat-val.min { color: #185FA5; }
.stat-val.avg { color: var(--green); }
.stat-val.max { color: #D85A30; }

.chart-card { background: var(--bg-card); border: 0.5px solid var(--border); border-radius: var(--radius-md); padding: 14px; margin-bottom: 12px; }
.chart-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.chart-title { font-size: 13px; font-weight: 500; }
.chart-now { font-size: 13px; color: var(--text-sec); }
.chart-now span { font-weight: 500; color: var(--text); }
.chart-wrap { position: relative; height: 160px; }

/* Actividad relays */
.relay-card { background: var(--bg-card); border: 0.5px solid var(--border); border-radius: var(--radius-md); padding: 12px; }
.relay-row { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.relay-row:last-child { margin-bottom: 0; }
.relay-name { font-size: 12px; color: var(--text-sec); width: 90px; flex-shrink: 0; }
.relay-track { flex: 1; height: 10px; background: var(--bg-sub); border-radius: 5px; overflow: hidden; display: flex; gap: 1px; }
.relay-seg { height: 100%; border-radius: 2px; flex: 1; }
.relay-seg.on  { background: var(--green); }
.relay-seg.off { background: transparent; }
.relay-pct { font-size: 11px; color: var(--text-ter); width: 32px; text-align: right; flex-shrink: 0; }

/* ── Configuración ── */
.config-section { background: var(--bg-card); border: 0.5px solid var(--border); border-radius: var(--radius-md); margin-bottom: 8px; overflow: hidden; }
.config-header { padding: 14px; display: flex; align-items: center; gap: 10px; cursor: pointer; user-select: none; }
.config-icon { width: 34px; height: 34px; border-radius: var(--radius-sm); display: flex; align-items: center; justify-content: center; flex-shrink: 0; }
.config-icon svg { width: 17px; height: 17px; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; fill: none; }
.config-name { font-size: 14px; font-weight: 500; }
.config-sub { font-size: 12px; color: var(--text-ter); margin-top: 1px; }
.chevron { width: 16px; height: 16px; stroke: var(--text-ter); fill: none; stroke-width: 2; stroke-linecap: round; transition: transform .2s; flex-shrink: 0; }
.chevron.open { transform: rotate(180deg); }
.config-body { border-top: 0.5px solid var(--border); padding: 14px; display: none; }
.config-body.open { display: block; }

.day-night { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 12px; }
.dn-card { background: var(--bg-sub); border-radius: var(--radius-sm); padding: 10px; }
.dn-lbl { font-size: 11px; color: var(--text-ter); font-weight: 500; text-transform: uppercase; letter-spacing: .04em; margin-bottom: 8px; }
.dn-field { display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px; }
.dn-field:last-child { margin-bottom: 0; }
.dn-field label { font-size: 12px; color: var(--text-sec); }
.dn-num {
  width: 48px; padding: 4px 6px; border: 0.5px solid var(--border-md);
  border-radius: 6px; background: var(--bg-card); color: var(--text);
  font-size: 13px; font-weight: 500; text-align: center;
}

.field-row { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.field-row:last-of-type { margin-bottom: 0; }
.field-label { font-size: 13px; color: var(--text-sec); }
.field-label small { display: block; font-size: 11px; color: var(--text-ter); margin-top: 1px; }
.num-input {
  width: 56px; padding: 6px 8px; border: 0.5px solid var(--border-md);
  border-radius: var(--radius-sm); background: var(--bg-sub); color: var(--text);
  font-size: 14px; font-weight: 500; text-align: center;
}
.unit { font-size: 12px; color: var(--text-ter); min-width: 20px; }
.time-pair { display: flex; align-items: center; gap: 4px; }
.time-sep { font-size: 14px; color: var(--text-ter); }

.divider { height: 0.5px; background: var(--border); margin: 12px 0; }

.day-pills { display: flex; gap: 5px; flex-wrap: wrap; }
.day-pill {
  width: 34px; height: 34px; border-radius: 50%;
  border: 0.5px solid var(--border); background: var(--bg-sub);
  font-size: 12px; font-weight: 500; display: flex; align-items: center; justify-content: center;
  cursor: pointer; color: var(--text-sec); transition: all .15s;
}
.day-pill.on { background: var(--green); border-color: var(--green); color: #fff; }

.toggle-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 9px 0; border-bottom: 0.5px solid var(--border);
}
.toggle-row:last-of-type { border-bottom: none; padding-bottom: 0; }
.toggle-row label { font-size: 13px; color: var(--text-sec); flex: 1; cursor: pointer; }
.tog {
  width: 38px; height: 22px; background: var(--border-md);
  border-radius: 11px; position: relative; cursor: pointer; transition: background .2s; flex-shrink: 0;
}
.tog.on { background: var(--green); }
.tog::after {
  content: ''; position: absolute;
  width: 18px; height: 18px; background: white; border-radius: 50%;
  top: 2px; left: 2px; transition: transform .2s;
}
.tog.on::after { transform: translateX(16px); }

.save-btn {
  width: 100%; margin-top: 14px; padding: 11px;
  background: var(--green); color: white; border: none;
  border-radius: var(--radius-sm); font-size: 14px; font-weight: 500;
  cursor: pointer; transition: opacity .15s;
}
.save-btn:hover { opacity: .88; }
.save-btn:active { opacity: .75; transform: scale(.99); }
.save-btn:disabled { opacity: .5; cursor: default; }

.saved-ok {
  display: none; align-items: center; justify-content: center;
  gap: 6px; color: var(--green); font-size: 13px; margin-top: 8px;
}
.saved-ok svg { width: 14px; height: 14px; stroke: var(--green); fill: none; stroke-width: 2.5; stroke-linecap: round; }

/* Mini gráfica del dashboard */
.mini-metric-btn {
  padding: 4px 10px;
  border-radius: 20px;
  border: 0.5px solid var(--border);
  background: var(--bg-sub);
  font-size: 11px;
  font-weight: 500;
  cursor: pointer;
  color: var(--text-sec);
  transition: all .15s;
}
.mini-metric-btn.active {
  background: var(--green);
  border-color: var(--green);
  color: #fff;
}
.mini-stat {
  flex: 1;
  text-align: center;
  padding: 8px 4px;
  background: var(--bg-sub);
  border-radius: var(--radius-sm);
}
.mini-stat-lbl { font-size: 11px; color: var(--text-ter); margin-bottom: 3px; }
.mini-stat-val { font-size: 15px; font-weight: 500; }
.mini-stat-val.min { color: #185FA5; }
.mini-stat-val.avg { color: var(--green); }
.mini-stat-val.max { color: #D85A30; }

/* Login */
.login-wrap {
  min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px;
}
.login-card {
  background: var(--bg-card); border: 0.5px solid var(--border);
  border-radius: var(--radius-lg); padding: 32px 24px; width: 100%; max-width: 360px;
}
.login-logo { text-align: center; margin-bottom: 28px; }
.login-logo img { display: block; margin: 0 auto 14px; }
.login-logo .logo-circle {
  width: 56px; height: 56px; background: var(--green);
  border-radius: 50%; display: flex; align-items: center; justify-content: center; margin: 0 auto 12px;
}
.login-logo .logo-circle svg { width: 28px; height: 28px; fill: white; }
.login-logo h1 { font-size: 20px; font-weight: 500; }
.login-logo p { font-size: 13px; color: var(--text-ter); margin-top: 2px; }
.form-group { margin-bottom: 14px; }
.form-group label { display: block; font-size: 13px; color: var(--text-sec); margin-bottom: 5px; }
.form-group input {
  width: 100%; padding: 10px 12px; border: 0.5px solid var(--border-md);
  border-radius: var(--radius-sm); background: var(--bg-sub); color: var(--text);
  font-size: 14px; transition: border-color .15s;
}
.form-group input:focus { outline: none; border-color: var(--green); }
.login-btn {
  width: 100%; padding: 11px; background: var(--green); color: white;
  border: none; border-radius: var(--radius-sm); font-size: 14px; font-weight: 500;
  cursor: pointer; margin-top: 8px; transition: opacity .15s;
}
.login-btn:hover { opacity: .88; }
.error-msg { color: #D85A30; font-size: 13px; text-align: center; margin-top: 10px; min-height: 20px; }
.forgot-link { display: block; text-align: center; font-size: 12px; color: var(--text-ter); margin-top: 14px; text-decoration: none; }
.forgot-link:hover { color: var(--text-sec); }
