/* =====================================================
   IoT Dashboard — Estilos
   ===================================================== */

:root {
  --bg-primary   : #0f172a;
  --bg-card      : #1e293b;
  --bg-card-alt  : #162032;
  --border       : #334155;
  --text-primary : #f1f5f9;
  --text-muted   : #94a3b8;
  --accent-blue  : #38bdf8;
  --accent-green : #4ade80;
  --accent-orange: #fb923c;
  --accent-red   : #f87171;
  --accent-purple: #c084fc;
  --radius       : 12px;
  --shadow       : 0 4px 24px rgba(0,0,0,0.4);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  background   : var(--bg-primary);
  color        : var(--text-primary);
  font-family  : 'Inter', system-ui, sans-serif;
  min-height   : 100vh;
  line-height  : 1.6;
}

/* ── Header ─────────────────────────────────────────── */
header {
  background   : var(--bg-card);
  border-bottom: 1px solid var(--border);
  padding      : 16px 32px;
  display      : flex;
  align-items  : center;
  justify-content: space-between;
  flex-wrap    : wrap;
  gap          : 12px;
}

.header-left h1 {
  font-size  : 1.4rem;
  font-weight: 700;
  color      : var(--accent-blue);
  display    : flex;
  align-items: center;
  gap        : 10px;
}

.header-left p {
  font-size: 0.8rem;
  color    : var(--text-muted);
}

.header-right {
  display    : flex;
  align-items: center;
  gap        : 16px;
}

.status-dot {
  width        : 10px;
  height       : 10px;
  border-radius: 50%;
  background   : var(--accent-green);
  box-shadow   : 0 0 8px var(--accent-green);
  animation    : pulse 2s infinite;
}

.status-dot.offline { background: var(--accent-red); box-shadow: 0 0 8px var(--accent-red); animation: none; }

@keyframes pulse {
  0%,100% { opacity: 1; }
  50%      { opacity: 0.4; }
}

.last-update {
  font-size: 0.78rem;
  color    : var(--text-muted);
}

/* ── Layout ─────────────────────────────────────────── */
.container {
  max-width: 1400px;
  margin   : 0 auto;
  padding  : 24px 20px;
}

/* ── Nav Menu ────────────────────────────────────────── */
.main-nav {
  display         : flex;
  background      : #080f1e;
  border-bottom   : 2px solid #6366f1;
  box-shadow      : 0 4px 24px rgba(99,102,241,.25);
  padding         : 0 24px;
  gap             : 4px;
  position        : sticky;
  top             : 0;
  z-index         : 100;
}

.nav-btn {
  display     : flex;
  align-items : center;
  gap         : 8px;
  padding     : 13px 20px;
  background  : none;
  border      : none;
  border-bottom: 3px solid transparent;
  color       : #64748b;
  font-size   : .9rem;
  font-weight : 500;
  cursor      : pointer;
  transition  : all .2s;
  white-space : nowrap;
  font-family : inherit;
  border-radius: 8px 8px 0 0;
}

.nav-btn:hover {
  color      : #f1f5f9;
  background : rgba(99,102,241,.12);
}

.nav-btn.active {
  color        : #ffffff;
  background   : linear-gradient(135deg, #6366f1 0%, #38bdf8 100%);
  border-bottom-color: transparent;
  font-weight  : 700;
  box-shadow   : 0 2px 16px rgba(99,102,241,.45);
  border-radius: 8px 8px 0 0;
}

/* Secciones */
.section { display: none; }
.section.active { display: block; }

@media (max-width: 600px) {
  .main-nav   { padding: 0 6px; overflow-x: auto; gap: 2px; }
  .nav-btn    { padding: 11px 13px; font-size: .8rem; gap: 5px; }
  .nav-btn svg{ display: none; }
}

/* ── Period Selector ─────────────────────────────────── */
.period-selector {
  display      : flex;
  gap          : 8px;
  margin-bottom: 24px;
  flex-wrap    : wrap;
}

.period-btn {
  padding     : 8px 20px;
  border-radius: 8px;
  border      : 1px solid var(--border);
  background  : var(--bg-card);
  color       : var(--text-muted);
  cursor      : pointer;
  font-size   : 0.85rem;
  font-weight : 500;
  transition  : all .2s;
}

.period-btn:hover  { border-color: var(--accent-blue); color: var(--accent-blue); }
.period-btn.active { background: var(--accent-blue); color: #0f172a; border-color: var(--accent-blue); font-weight: 600; }

/* ── Stat Cards ─────────────────────────────────────── */
.cards-grid {
  display              : grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap                  : 16px;
  margin-bottom        : 24px;
}

.card {
  background   : var(--bg-card);
  border       : 1px solid var(--border);
  border-radius: var(--radius);
  padding      : 20px 24px;
  box-shadow   : var(--shadow);
  transition   : transform .2s;
}
.card:hover { transform: translateY(-2px); }

.card-label {
  font-size  : 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .08em;
  color      : var(--text-muted);
  margin-bottom: 8px;
}

.card-value {
  font-size  : 2.2rem;
  font-weight: 700;
  line-height: 1;
}

.card-sub {
  font-size: 0.78rem;
  color    : var(--text-muted);
  margin-top: 6px;
}

.card-icon {
  font-size  : 1.8rem;
  margin-bottom: 10px;
}

.temp-color   { color: var(--accent-orange); }
.humid-color  { color: var(--accent-blue); }
.green-color  { color: var(--accent-green); }
.purple-color { color: var(--accent-purple); }

/* ── Charts ─────────────────────────────────────────── */
.charts-grid {
  display              : grid;
  grid-template-columns: 1fr 1fr;
  gap                  : 20px;
  margin-bottom        : 24px;
}

@media (max-width: 900px) {
  .charts-grid { grid-template-columns: 1fr; }
}

.chart-card {
  background   : var(--bg-card);
  border       : 1px solid var(--border);
  border-radius: var(--radius);
  padding      : 20px;
  box-shadow   : var(--shadow);
}

.chart-title {
  font-size    : 0.9rem;
  font-weight  : 600;
  color        : var(--text-muted);
  margin-bottom: 16px;
  text-transform: uppercase;
  letter-spacing: .05em;
}

.chart-wrapper {
  position: relative;
  height  : 240px;
}

/* ── AI Analysis Panel ──────────────────────────────── */
.ai-panel {
  background   : var(--bg-card);
  border       : 1px solid var(--border);
  border-radius: var(--radius);
  padding      : 24px;
  box-shadow   : var(--shadow);
  margin-bottom: 24px;
}

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

.ai-panel-header h2 {
  font-size  : 1.05rem;
  font-weight: 700;
  display    : flex;
  align-items: center;
  gap        : 8px;
}

.ai-period-tabs {
  display: flex;
  gap    : 6px;
}

.ai-tab {
  padding     : 5px 14px;
  border-radius: 6px;
  border      : 1px solid var(--border);
  background  : transparent;
  color       : var(--text-muted);
  cursor      : pointer;
  font-size   : 0.8rem;
  transition  : all .2s;
}
.ai-tab:hover  { border-color: var(--accent-purple); color: var(--accent-purple); }
.ai-tab.active { background: var(--accent-purple); color: #0f172a; border-color: var(--accent-purple); font-weight: 600; }

.ai-content-grid {
  display              : grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap                  : 16px;
}

@media (max-width: 900px) {
  .ai-content-grid { grid-template-columns: 1fr; }
}

.ai-section h3 {
  font-size    : 0.78rem;
  font-weight  : 600;
  text-transform: uppercase;
  letter-spacing: .08em;
  color        : var(--text-muted);
  margin-bottom: 10px;
}

.ai-summary {
  font-size  : 0.9rem;
  line-height: 1.7;
  color      : var(--text-primary);
  background : var(--bg-card-alt);
  border-radius: 8px;
  padding    : 12px 16px;
  border-left: 3px solid var(--accent-purple);
  min-height : 80px;
}

.ai-list {
  list-style: none;
  display   : flex;
  flex-direction: column;
  gap       : 8px;
}

.ai-list li {
  font-size    : 0.85rem;
  padding      : 8px 12px;
  border-radius: 8px;
  background   : var(--bg-card-alt);
  display      : flex;
  align-items  : flex-start;
  gap          : 8px;
}

.ai-list.alerts li  { border-left: 3px solid var(--accent-red);   }
.ai-list.recs   li  { border-left: 3px solid var(--accent-green);  }

.ai-empty {
  font-size : 0.82rem;
  color     : var(--text-muted);
  font-style: italic;
  padding   : 8px 0;
}

.ai-trigger-btn {
  padding     : 8px 18px;
  border-radius: 8px;
  border      : 1px solid var(--accent-purple);
  background  : transparent;
  color       : var(--accent-purple);
  cursor      : pointer;
  font-size   : 0.82rem;
  font-weight : 600;
  transition  : all .2s;
}
.ai-trigger-btn:hover    { background: var(--accent-purple); color: #0f172a; }
.ai-trigger-btn:disabled { opacity: .5; cursor: not-allowed; }

/* ── Loading skeleton ─────────────────────────────────── */
.skeleton {
  background        : linear-gradient(90deg, var(--bg-card-alt) 25%, var(--border) 50%, var(--bg-card-alt) 75%);
  background-size   : 200% 100%;
  animation         : shimmer 1.5s infinite;
  border-radius     : 6px;
  height            : 1em;
  display           : inline-block;
  width             : 100%;
}

@keyframes shimmer {
  0%   { background-position: -200% 0; }
  100% { background-position:  200% 0; }
}

/* ── Toast notifications ──────────────────────────────── */
#toast-container {
  position  : fixed;
  bottom    : 24px;
  right     : 24px;
  display   : flex;
  flex-direction: column;
  gap       : 8px;
  z-index   : 1000;
}

.toast {
  background   : var(--bg-card);
  border       : 1px solid var(--border);
  border-radius: 10px;
  padding      : 12px 18px;
  font-size    : 0.85rem;
  min-width    : 240px;
  box-shadow   : var(--shadow);
  animation    : slideIn .3s ease;
  display      : flex;
  align-items  : center;
  gap          : 10px;
}
.toast.success { border-left: 4px solid var(--accent-green); }
.toast.error   { border-left: 4px solid var(--accent-red);   }
.toast.info    { border-left: 4px solid var(--accent-blue);  }

@keyframes slideIn {
  from { transform: translateX(100%); opacity: 0; }
  to   { transform: translateX(0);    opacity: 1; }
}

/* ── Panel de Control ────────────────────────────────── */
.control-grid {
  display              : grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap                  : 20px;
}

.control-card {
  background   : var(--bg-card-alt);
  border       : 2px solid var(--border);
  border-radius: 14px;
  padding      : 22px;
  transition   : border-color .25s, box-shadow .25s;
}

.control-card.active {
  border-color: var(--accent-green);
  box-shadow  : 0 0 20px rgba(74,222,128,0.18);
}

.control-top {
  display    : flex;
  align-items: center;
  gap        : 14px;
  margin-bottom: 18px;
}

.control-icon { font-size: 2rem; flex-shrink: 0; }

.control-name {
  font-weight: 700;
  font-size  : 1rem;
  color      : var(--text-primary);
}
.control-pin {
  font-size : .78rem;
  color     : var(--text-muted);
  margin-top: 2px;
}

.control-badge {
  margin-left  : auto;
  padding      : 4px 10px;
  border-radius: 20px;
  font-size    : .72rem;
  font-weight  : 700;
  letter-spacing: .06em;
}
.control-badge.on  {
  background: rgba(74,222,128,0.15);
  color     : var(--accent-green);
  border    : 1px solid var(--accent-green);
}
.control-badge.off {
  background: rgba(100,116,139,0.15);
  color     : var(--text-muted);
  border    : 1px solid var(--border);
}

.control-btn {
  width        : 100%;
  padding      : 13px;
  border-radius: 10px;
  border       : none;
  font-size    : .95rem;
  font-weight  : 700;
  cursor       : pointer;
  letter-spacing: .05em;
  transition   : all .2s;
}

.control-btn.off {
  background: rgba(56,189,248,0.12);
  color     : var(--accent-blue);
  border    : 2px solid var(--accent-blue);
}
.control-btn.off:hover {
  background: rgba(56,189,248,0.25);
}

.control-btn.on {
  background: var(--accent-green);
  color     : #0f172a;
  border    : 2px solid var(--accent-green);
  box-shadow: 0 0 18px rgba(74,222,128,0.35);
}
.control-btn.on:hover { opacity: .88; }

.control-btn:disabled {
  opacity: .5;
  cursor : not-allowed;
}

/* ── Botones de duración relay ───────────────────────── */
.dur-btn {
  padding      : 5px 10px;
  border-radius: 6px;
  border       : 1px solid #334155;
  background   : transparent;
  color        : #64748b;
  font-size    : .78rem;
  font-weight  : 600;
  cursor       : pointer;
  font-family  : inherit;
  transition   : all .15s;
}
.dur-btn:hover  { border-color: #38bdf8; color: #38bdf8; }
.dur-btn.active { border-color: #38bdf8; background: rgba(56,189,248,.15); color: #38bdf8; }

/* ── Panel de Alertas ─────────────────────────────────── */
.alerts-list {
  display       : flex;
  flex-direction: column;
  gap           : 8px;
}

.alert-item {
  display        : flex;
  align-items    : center;
  gap            : 14px;
  padding        : 12px 16px;
  background     : var(--bg-card-alt);
  border         : 1px solid var(--border);
  border-radius  : 8px;
  border-left    : 4px solid var(--accent-orange);
  font-size      : .9rem;
}

.alert-item.temp_high  { border-left-color: #f87171; }
.alert-item.temp_low   { border-left-color: #38bdf8; }
.alert-item.humid_high { border-left-color: #fb923c; }

.alert-icon   { font-size: 1.4rem; flex-shrink: 0; }
.alert-body   { flex: 1; }
.alert-title  { font-weight: 600; color: var(--text-primary); margin-bottom: 2px; }
.alert-meta   { font-size: .78rem; color: var(--text-muted); }
.alert-value  { font-size: 1.1rem; font-weight: 700; color: var(--accent-orange); flex-shrink: 0; }

.alert-item.temp_high  .alert-value { color: #f87171; }
.alert-item.temp_low   .alert-value { color: #38bdf8; }
.alert-item.humid_high .alert-value { color: #fb923c; }

.alerts-empty {
  text-align : center;
  color      : var(--text-muted);
  padding    : 24px;
  font-size  : .9rem;
}

/* ── Chat IA ─────────────────────────────────────────── */

.chat-msg {
  display       : flex;
  flex-direction: column;
  max-width     : 80%;
}

.chat-msg.user { align-self: flex-end; align-items: flex-end; }
.chat-msg.bot  { align-self: flex-start; align-items: flex-start; }

.chat-bubble {
  padding      : .65rem 1rem;
  border-radius: 14px;
  font-size    : .9rem;
  line-height  : 1.5;
  word-break   : break-word;
}

.user-bubble {
  background: linear-gradient(135deg, #6366f1, #4f46e5);
  color      : white;
  border-bottom-right-radius: 4px;
}

.bot-bubble {
  background: #1e293b;
  color      : #f1f5f9;
  border     : 1px solid #334155;
  border-bottom-left-radius: 4px;
}

.chat-time {
  font-size : .7rem;
  color     : #475569;
  margin-top: 3px;
  padding   : 0 4px;
}

.chat-typing {
  display    : flex;
  gap        : 4px;
  align-items: center;
  padding    : .65rem 1rem;
  background : #1e293b;
  border     : 1px solid #334155;
  border-radius: 14px;
  border-bottom-left-radius: 4px;
  width      : fit-content;
}

.chat-typing span {
  width           : 7px;
  height          : 7px;
  background      : #6366f1;
  border-radius   : 50%;
  animation       : typingDot .9s infinite;
}

.chat-typing span:nth-child(2) { animation-delay: .2s; }
.chat-typing span:nth-child(3) { animation-delay: .4s; }

@keyframes typingDot {
  0%, 60%, 100% { opacity: .3; transform: scale(1); }
  30%           { opacity: 1;  transform: scale(1.3); }
}

.chat-suggestion {
  background   : #1e293b;
  color        : #94a3b8;
  border       : 1px solid #334155;
  border-radius: 20px;
  padding      : .35rem .9rem;
  font-size    : .78rem;
  cursor       : pointer;
  transition   : all .2s;
  font-family  : inherit;
}

.chat-suggestion:hover {
  background  : #6366f1;
  color       : white;
  border-color: #6366f1;
}

/* ── Responsive Mobile ───────────────────────────────── */
@media (max-width: 600px) {

  header {
    flex-direction: column;
    align-items   : flex-start;
    gap           : 8px;
    padding       : 14px 16px;
  }

  .header-right {
    width      : 100%;
    justify-content: space-between;
  }

  .container { padding: 16px 12px; }

  .period-selector { justify-content: center; }

  .cards-grid {
    grid-template-columns: 1fr 1fr;
  }

  .card { padding: 16px 12px; }
  .card-value { font-size: 1.6rem; }

  .ai-panel-header {
    flex-direction: column;
    align-items   : flex-start;
    gap           : 12px;
  }

  .ai-panel-header > div {
    width: 100%;
    justify-content: space-between;
  }

  .ai-content-grid { grid-template-columns: 1fr; }

  .alert-item { flex-wrap: wrap; gap: 8px; }

  .chart-wrapper { height: 180px; }
}
