:root {
  color-scheme: dark;
  --bg: #07111f;
  --panel: #0f172a;
  --panel-2: #111c31;
  --border: rgba(255,255,255,0.09);
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #38bdf8;
  --accent-2: #22c55e;
  --danger: #f43f5e;
  --warning: #fb923c;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: 'Inter', system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: radial-gradient(circle at top, #14223a 0%, var(--bg) 65%);
  color: var(--text);
  min-height: 100vh;
}
a { color: inherit; text-decoration: none; }
button, input, select, textarea { font: inherit; }
button { cursor: pointer; }

.app-shell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 280px 1fr;
}
.sidebar {
  background: rgba(4, 12, 24, 0.92);
  border-right: 1px solid var(--border);
  padding: 24px 18px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}
.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  font-weight: 700;
  letter-spacing: 0.02em;
}
.brand-mark {
  width: 42px;
  height: 42px;
  border-radius: 12px;
  background: linear-gradient(135deg, var(--accent), #6366f1);
  display: grid;
  place-items: center;
  font-size: 20px;
}
.nav-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 6px;
}
.nav-btn {
  text-align: left;
  border: 1px solid transparent;
  border-radius: 12px;
  padding: 12px 14px;
  color: var(--text);
  background: transparent;
  transition: background 180ms ease, border-color 180ms ease, transform 180ms ease;
}
.nav-btn:hover, .nav-btn.active {
  background: rgba(56, 189, 248, 0.14);
  border-color: rgba(56, 189, 248, 0.3);
  transform: translateX(2px);
}
.mobile-topbar {
  display: none;
}
.mobile-menu-toggle {
  display: none;
}
.sidebar-backdrop {
  display: none;
}
.main {
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}
.grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 16px;
}
.card {
  background: rgba(15, 23, 42, 0.86);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 16px;
  box-shadow: 0 12px 24px rgba(2,8,23,0.24);
}
.card h3, .card h4 { margin: 0 0 8px; font-size: 1rem; }
.subtle { color: var(--muted); font-size: 0.9rem; }
.stat-row { display: flex; justify-content: space-between; align-items: center; margin-top: 8px; }
.stat-number { font-size: 1.2rem; font-weight: 700; }
.device-list, .appointment-list, .trigger-list { display: grid; gap: 12px; }
.device-card, .item-card {
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 14px;
  background: linear-gradient(135deg, rgba(17, 28, 49, 0.96), rgba(15, 23, 42, 0.9));
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.04);
}
.item-head { display: flex; justify-content: space-between; gap: 12px; align-items: flex-start; }
.device-main {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.device-signal {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--accent-2);
  box-shadow: 0 0 0 4px rgba(34, 197, 94, 0.14);
}
.device-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 4px;
}
.type-badge {
  display: inline-flex;
  align-items: center;
  padding: 4px 8px;
  border-radius: 999px;
  background: rgba(56, 189, 248, 0.12);
  color: #bae6fd;
  font-size: 0.78rem;
  font-weight: 600;
}
.config-block {
  margin-top: 10px;
  padding: 10px 12px;
  border-radius: 12px;
  background: rgba(2, 8, 23, 0.38);
  border: 1px solid rgba(255,255,255,0.06);
}
.config-label {
  display: block;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--accent);
  margin-bottom: 4px;
}
.tag {
  display: inline-flex;
  padding: 6px 8px;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 600;
}
.tag.active { background: rgba(34, 197, 94, 0.16); color: #bbf7d0; }
.tag.inactive { background: rgba(244, 63, 94, 0.16); color: #fecdd3; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.btn, .btn-secondary, .btn-danger {
  border: none;
  border-radius: 999px;
  padding: 8px 12px;
  color: white;
  background: var(--accent);
}
.btn-secondary { background: #334155; }
.btn-danger { background: var(--danger); }
.form-grid { display: grid; gap: 12px; }
.form-section {
  padding: 14px;
  border-radius: 14px;
  background: rgba(2, 8, 23, 0.34);
  border: 1px solid rgba(255,255,255,0.06);
}
.form-section-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.92rem;
  font-weight: 600;
  margin-bottom: 10px;
  color: #e2e8f0;
}
.field { display: flex; flex-direction: column; gap: 6px; }
.field label { font-size: 0.9rem; color: var(--muted); }
.field input, .field select, .field textarea {
  width: 100%;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: rgba(15, 23, 42, 0.9);
  color: var(--text);
  padding: 10px 12px;
}
.field textarea { min-height: 88px; resize: vertical; }
.inline-fields { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.section-title { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.empty-state {
  padding: 20px;
  border: 1px dashed var(--border);
  border-radius: 12px;
  color: var(--muted);
  text-align: center;
}
.muted { color: var(--muted); }
.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  border-radius: 999px;
  font-size: 0.8rem;
  background: rgba(56, 189, 248, 0.12);
  color: #bae6fd;
}
.small { font-size: 0.85rem; }
.table-wrap {
  overflow-x: auto;
  margin-top: 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
}
.log-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
  white-space: nowrap;
}
.log-table th, .log-table td {
  text-align: left;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}
.log-table th {
  background: rgba(2, 8, 23, 0.5);
  color: var(--muted);
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  position: sticky;
  top: 0;
}
.log-table tbody tr:hover { background: rgba(56, 189, 248, 0.06); }
.log-table tbody tr:last-child td { border-bottom: none; }
.pagination {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  flex-wrap: wrap;
  gap: 12px;
  margin-top: 14px;
}
.pagination-controls {
  display: flex;
  align-items: center;
  gap: 12px;
}
/* Specificity note: the "Sistema" group wraps this title in <section className="card">
   (single-card layout, unlike the plain <section> used by the other dashboard groups), so this
   needs to outrank ".card h3, .card h4 { margin/font-size }" above — hence the extra ".card h3"
   selector alongside the plain class selector used by the other groups. */
.card h3.dashboard-group-title, .dashboard-group-title {
  margin: 18px 0 10px;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}
.card h3.dashboard-group-title:first-child, .dashboard-group-title:first-child { margin-top: 0; }
.dashboard-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 14px;
}
.sensor-card {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 16px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: linear-gradient(135deg, rgba(17, 28, 49, 0.96), rgba(15, 23, 42, 0.9));
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.04);
}
.sensor-card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}
.sensor-readings {
  display: flex;
  flex-wrap: wrap;
  gap: 18px;
}
.sensor-reading {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 1.1rem;
  font-weight: 600;
}
.sensor-reading .icon { font-size: 1.25rem; }
.refresh-btn {
  border: none;
  background: rgba(56, 189, 248, 0.14);
  color: var(--accent);
  border-radius: 999px;
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  font-size: 1rem;
  flex-shrink: 0;
}
.refresh-btn:hover { background: rgba(56, 189, 248, 0.26); }
.relay-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: linear-gradient(135deg, rgba(17, 28, 49, 0.96), rgba(15, 23, 42, 0.9));
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.04);
}
.relay-card-name { font-weight: 600; }
.system-metric-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 10px;
}
.system-metric-tile {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: rgba(2, 8, 23, 0.34);
}
.system-metric-tile .icon { font-size: 1.1rem; flex-shrink: 0; }
.system-metric-tile-body {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}
.system-metric-tile-name {
  font-size: 0.78rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.system-metric-tile-value { font-weight: 600; font-size: 0.95rem; }
.toggle-switch {
  position: relative;
  display: inline-block;
  width: 46px;
  height: 26px;
  flex-shrink: 0;
}
.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}
.toggle-slider {
  position: absolute;
  inset: 0;
  cursor: pointer;
  background: rgba(255,255,255,0.14);
  border: 1px solid var(--border);
  border-radius: 999px;
  transition: background 160ms ease;
}
.toggle-slider::before {
  content: "";
  position: absolute;
  height: 20px;
  width: 20px;
  left: 2px;
  top: 2px;
  background: white;
  border-radius: 50%;
  transition: transform 160ms ease;
}
.toggle-switch input:checked + .toggle-slider {
  background: var(--accent-2);
  border-color: transparent;
}
.toggle-switch input:checked + .toggle-slider::before {
  transform: translateX(20px);
}
@media (max-width: 960px) {
  .app-shell { grid-template-columns: 1fr; }
  .sidebar { border-right: none; border-bottom: 1px solid var(--border); }
  .grid { grid-template-columns: 1fr; }
}
@media (max-width: 768px) {
  .mobile-topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 64px;
    padding: 0 14px;
    background: rgba(4, 12, 24, 0.95);
    border-bottom: 1px solid var(--border);
    z-index: 60;
  }
  .app-shell { padding-top: 64px; }
  .sidebar .brand { display: none; }
  .mobile-menu-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    width: 40px;
    height: 40px;
    border-radius: 12px;
    border: 1px solid var(--border);
    background: rgba(15, 23, 42, 0.92);
    color: var(--text);
    font-size: 1.2rem;
  }
  .sidebar-backdrop {
    display: block;
    position: fixed;
    top: 64px;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(2, 8, 23, 0.55);
    z-index: 50;
  }
  .sidebar {
    position: fixed;
    top: 64px;
    right: 0;
    height: calc(100vh - 64px);
    width: min(280px, 80vw);
    border-left: 1px solid var(--border);
    border-bottom: none;
    transform: translateX(100%);
    transition: transform 220ms ease;
    z-index: 55;
    overflow-y: auto;
    box-shadow: -12px 0 32px rgba(2,8,23,0.5);
  }
  .sidebar.sidebar-open {
    transform: translateX(0);
  }
}
.checkbox-pill-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 16px;
}
.checkbox-pill {
  display: flex;
  align-items: center;
  gap: 6px;
  font-weight: 400;
  font-size: 0.92rem;
}
.chart-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(420px, 1fr));
  gap: 16px;
  margin-top: 14px;
}
.chart-card {
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 14px;
  background: rgba(2, 8, 23, 0.34);
}
.chart-canvas-wrap {
  position: relative;
  height: 300px;
  margin-top: 10px;
}
@media (max-width: 640px) {
  .main { padding: 14px; }
  .inline-fields { grid-template-columns: 1fr; }
  .item-head, .device-main { flex-direction: column; align-items: flex-start; }
  .system-metric-grid { grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); }
  .actions { gap: 6px; }
  .btn, .btn-secondary, .btn-danger { width: 100%; }
  .form-section { padding: 12px; }
  .chart-grid { grid-template-columns: 1fr; }
  .chart-canvas-wrap { height: 240px; }
}
