:root {
  --bg: #070b16;
  --bg-2: #0c1324;
  --surface: rgba(19, 28, 51, 0.72);
  --surface-solid: #121a30;
  --surface-2: #18223d;
  --border: rgba(122, 162, 255, 0.14);
  --border-strong: rgba(122, 162, 255, 0.3);
  --text: #e7ecf8;
  --muted: #8d9bbd;
  --faint: #5d6a8a;
  --accent: #38bdf8;
  --accent-2: #8b5cf6;
  --ok: #34d399;
  --warn: #fbbf24;
  --err: #f87171;
  --radius: 16px;
  --radius-sm: 10px;
  --shadow: 0 10px 40px rgba(0, 0, 0, 0.35);
  --nav-h: 68px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  background: radial-gradient(1200px 600px at 10% -10%, rgba(56, 189, 248, 0.12), transparent 60%),
              radial-gradient(900px 500px at 110% 10%, rgba(139, 92, 246, 0.14), transparent 60%),
              var(--bg);
  background-attachment: fixed;
  color: var(--text);
  font: 15px/1.5 "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); }
button, input, select { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; letter-spacing: -0.01em; }
h1 { font-size: 26px; font-weight: 700; }
h2 { font-size: 18px; font-weight: 650; }
h3 { font-size: 15px; font-weight: 600; }
.muted { color: var(--muted); }
.faint { color: var(--faint); }
.mono { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: 12.5px; }
.hidden { display: none !important; }

.boot { min-height: 100vh; display: grid; place-items: center; }
.spinner { width: 34px; height: 34px; border-radius: 50%; border: 3px solid var(--border-strong); border-top-color: var(--accent); animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Layout ---------- */
.shell { display: grid; grid-template-columns: 248px 1fr; min-height: 100vh; }
.sidebar {
  position: sticky; top: 0; height: 100vh; padding: 22px 16px;
  border-right: 1px solid var(--border); background: rgba(7, 11, 22, 0.6); backdrop-filter: blur(14px);
  display: flex; flex-direction: column; gap: 22px;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 0 8px; }
.brand img { width: 34px; height: 34px; }
.brand strong { display: block; font-size: 15px; }
.brand span { font-size: 12px; color: var(--muted); }
.nav { display: flex; flex-direction: column; gap: 4px; }
.nav a {
  display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-radius: 12px;
  color: var(--muted); text-decoration: none; font-weight: 550;
}
.nav a svg { width: 20px; height: 20px; flex: none; }
.nav a:hover { color: var(--text); background: rgba(122, 162, 255, 0.06); }
.nav a.active { color: var(--text); background: linear-gradient(90deg, rgba(56, 189, 248, 0.16), rgba(139, 92, 246, 0.1)); box-shadow: inset 0 0 0 1px var(--border); }
.sidebar .spacer { flex: 1; }
.sidebar .who { font-size: 13px; color: var(--muted); padding: 0 8px; display: flex; justify-content: space-between; align-items: center; gap: 8px; }

main { padding: 28px clamp(16px, 4vw, 40px) 48px; max-width: 1240px; width: 100%; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 22px; flex-wrap: wrap; }
.page-head p { margin: 4px 0 0; color: var(--muted); }

@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; top: auto; bottom: 0; left: 0; right: 0; height: calc(var(--nav-h) + env(safe-area-inset-bottom));
    padding: 6px 8px calc(6px + env(safe-area-inset-bottom)); flex-direction: row; border-right: 0; border-top: 1px solid var(--border);
    z-index: 20; background: rgba(7, 11, 22, 0.92);
  }
  .sidebar .brand, .sidebar .spacer, .sidebar .who { display: none; }
  .nav { flex-direction: row; justify-content: space-around; width: 100%; }
  .nav a { flex-direction: column; gap: 2px; font-size: 11px; padding: 6px 4px; flex: 1; justify-content: center; text-align: center; }
  main { padding: 20px 16px calc(var(--nav-h) + 32px + env(safe-area-inset-bottom)); }
  .mobile-top { display: flex !important; }
  h1 { font-size: 22px; }
}
.mobile-top { display: none; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.mobile-top .brand { padding: 0; }

/* ---------- Componentes ---------- */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px; backdrop-filter: blur(10px); box-shadow: var(--shadow);
}
.grid { display: grid; gap: 16px; }
.grid.cols-3 { grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }
.grid.cols-2 { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }
@media (max-width: 420px) { .grid.cols-2 { grid-template-columns: 1fr; } }
.stack { display: flex; flex-direction: column; gap: 12px; }
.row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.between { justify-content: space-between; }
.section { margin-top: 28px; }
.section > h2 { margin-bottom: 12px; }

.space-card { position: relative; overflow: hidden; text-decoration: none; color: inherit; display: block; transition: transform 0.15s, border-color 0.15s; }
.space-card:hover { transform: translateY(-2px); border-color: var(--border-strong); }
.space-card::after {
  content: ""; position: absolute; inset: auto -40px -60px auto; width: 180px; height: 180px; border-radius: 50%;
  background: radial-gradient(circle, rgba(56, 189, 248, 0.22), transparent 70%);
}
.space-card.farm::after { background: radial-gradient(circle, rgba(52, 211, 153, 0.22), transparent 70%); }
.space-card.pending::after { background: radial-gradient(circle, rgba(251, 191, 36, 0.2), transparent 70%); }
.space-card .big { font-size: 34px; font-weight: 750; margin: 10px 0 2px; }
.space-card .icon { width: 40px; height: 40px; border-radius: 12px; display: grid; place-items: center; background: rgba(56, 189, 248, 0.14); color: var(--accent); }
.space-card.farm .icon { background: rgba(52, 211, 153, 0.14); color: var(--ok); }
.space-card.pending .icon { background: rgba(251, 191, 36, 0.14); color: var(--warn); }
.space-card .icon svg { width: 22px; height: 22px; }

.badge {
  display: inline-flex; align-items: center; gap: 6px; padding: 3px 9px; border-radius: 999px;
  font-size: 12px; font-weight: 600; border: 1px solid var(--border); color: var(--muted); background: rgba(122, 162, 255, 0.05);
  white-space: nowrap;
}
.badge.ok { color: var(--ok); border-color: rgba(52, 211, 153, 0.35); background: rgba(52, 211, 153, 0.08); }
.badge.warn { color: var(--warn); border-color: rgba(251, 191, 36, 0.35); background: rgba(251, 191, 36, 0.08); }
.badge.err { color: var(--err); border-color: rgba(248, 113, 113, 0.35); background: rgba(248, 113, 113, 0.08); }
.badge.info { color: var(--accent); border-color: rgba(56, 189, 248, 0.35); background: rgba(56, 189, 248, 0.08); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: currentColor; display: inline-block; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; padding: 9px 16px; border-radius: 12px;
  border: 1px solid var(--border-strong); background: var(--surface-2); cursor: pointer; font-weight: 600; text-decoration: none; color: var(--text);
  min-height: 40px;
}
.btn:hover { border-color: var(--accent); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn.primary { border: 0; background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: #fff; box-shadow: 0 6px 20px rgba(56, 189, 248, 0.25); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--err); border-color: rgba(248, 113, 113, 0.4); background: transparent; }
.btn.sm { padding: 5px 11px; min-height: 32px; font-size: 13px; border-radius: 10px; }
.btn svg { width: 18px; height: 18px; }
.btn.loading::before { content: ""; width: 14px; height: 14px; border-radius: 50%; border: 2px solid rgba(255,255,255,0.4); border-top-color: #fff; animation: spin 0.8s linear infinite; }

.input, select.input {
  width: 100%; padding: 10px 12px; border-radius: 12px; border: 1px solid var(--border-strong); background: rgba(7, 11, 22, 0.6);
  outline: none; min-height: 42px;
}
.input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(56, 189, 248, 0.18); }
label.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); font-weight: 550; }
label.field small { color: var(--faint); font-weight: 400; }

.tabs { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 4px; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tab {
  border: 1px solid var(--border); background: transparent; padding: 7px 14px; border-radius: 999px; cursor: pointer; white-space: nowrap; color: var(--muted); font-weight: 600; font-size: 13px;
}
.tab.active { color: var(--text); border-color: var(--accent); background: rgba(56, 189, 248, 0.1); }
.tab .count { color: var(--faint); margin-left: 4px; }

.toolbar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin: 14px 0 16px; }
.toolbar .input { max-width: 320px; flex: 1 1 200px; }
.toolbar select.input { max-width: 200px; flex: 0 1 180px; }

/* ---------- Dispositivos ---------- */
.device-list { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); }
@media (max-width: 420px) { .device-list { grid-template-columns: 1fr; } }
.device {
  display: flex; flex-direction: column; gap: 10px; padding: 14px; cursor: pointer; position: relative;
  transition: border-color 0.15s;
}
.device:hover { border-color: var(--border-strong); }
.device.selected { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent) inset, var(--shadow); }
.device.unsupported { opacity: 0.8; }
.device .top { display: flex; gap: 12px; align-items: flex-start; }
.device .glyph { width: 40px; height: 40px; border-radius: 12px; display: grid; place-items: center; background: rgba(139, 92, 246, 0.14); color: #c4b5fd; flex: none; }
.device .glyph svg { width: 20px; height: 20px; }
.device .title { flex: 1; min-width: 0; }
.device .title h3 { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.device .title .orig { font-size: 12.5px; color: var(--faint); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.device .meta { display: grid; grid-template-columns: auto 1fr; gap: 3px 12px; font-size: 12.5px; }
.device .meta dt { color: var(--faint); }
.device .meta dd { margin: 0; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.device .foot { display: flex; gap: 6px; flex-wrap: wrap; }
.check {
  width: 22px; height: 22px; border-radius: 7px; border: 1.5px solid var(--border-strong); display: grid; place-items: center; flex: none; background: transparent; cursor: pointer; padding: 0;
}
.check.on { background: var(--accent); border-color: var(--accent); }
.check svg { width: 14px; height: 14px; color: #04121f; }

.bulkbar {
  position: sticky; bottom: 16px; z-index: 15; margin-top: 16px; display: flex; gap: 10px; align-items: center; flex-wrap: wrap;
  padding: 12px 14px; border-radius: var(--radius); background: var(--surface-solid); border: 1px solid var(--accent); box-shadow: var(--shadow);
}
@media (max-width: 860px) { .bulkbar { bottom: calc(var(--nav-h) + 12px + env(safe-area-inset-bottom)); } }
.bulkbar select.input { width: auto; flex: 1 1 140px; }

.empty { text-align: center; padding: 40px 20px; color: var(--muted); }
.empty svg { width: 42px; height: 42px; color: var(--faint); margin-bottom: 8px; }

/* ---------- Integrações ---------- */
.integration .head { display: flex; gap: 12px; align-items: center; }
.integration .logo { width: 44px; height: 44px; border-radius: 12px; display: grid; place-items: center; font-weight: 800; font-size: 15px; flex: none; }
.logo.tuya { background: rgba(255, 96, 56, 0.16); color: #ff8a65; }
.logo.ewelink { background: rgba(56, 189, 248, 0.16); color: var(--accent); }
.logo.avant { background: rgba(251, 191, 36, 0.14); color: var(--warn); }
.steps { margin: 0; padding-left: 20px; color: var(--muted); font-size: 13.5px; display: flex; flex-direction: column; gap: 6px; }
.alert { border-radius: 12px; padding: 10px 12px; font-size: 13.5px; border: 1px solid; }
.alert.err { border-color: rgba(248, 113, 113, 0.35); background: rgba(248, 113, 113, 0.08); color: #fecaca; }
.alert.warn { border-color: rgba(251, 191, 36, 0.35); background: rgba(251, 191, 36, 0.08); color: #fde68a; }
.alert.ok { border-color: rgba(52, 211, 153, 0.35); background: rgba(52, 211, 153, 0.08); color: #a7f3d0; }
.alert.info { border-color: rgba(56, 189, 248, 0.3); background: rgba(56, 189, 248, 0.07); color: #bae6fd; }
details.more summary { cursor: pointer; color: var(--accent); font-weight: 600; font-size: 13.5px; }
details.more[open] summary { margin-bottom: 10px; }
.copy { display: flex; gap: 6px; }
.copy .input { font-family: ui-monospace, Menlo, monospace; font-size: 12.5px; }
.kv { display: grid; grid-template-columns: repeat(auto-fit, minmax(100px, 1fr)); gap: 10px; }
.kv div { background: rgba(7, 11, 22, 0.45); border: 1px solid var(--border); border-radius: 12px; padding: 8px 10px; }
.kv b { display: block; font-size: 18px; }
.kv span { font-size: 12px; color: var(--faint); }

.list-row { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 10px 0; border-top: 1px solid var(--border); }
.list-row:first-child { border-top: 0; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }

/* ---------- Login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.login .card { width: min(400px, 100%); padding: 28px; }
.login .brand { padding: 0; margin-bottom: 22px; }
.login .brand img { width: 44px; height: 44px; }
.login .brand strong { font-size: 18px; }

/* ---------- Modal / toast ---------- */
dialog#modal {
  border: 1px solid var(--border-strong); border-radius: 20px; background: var(--surface-solid); color: var(--text); padding: 0;
  width: min(520px, calc(100vw - 32px)); box-shadow: var(--shadow); max-height: calc(100vh - 40px);
}
dialog#modal::backdrop { background: rgba(3, 6, 14, 0.7); backdrop-filter: blur(4px); }
.modal-body { padding: 22px; display: flex; flex-direction: column; gap: 14px; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; flex-wrap: wrap; }
#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(20px); opacity: 0; pointer-events: none;
  background: var(--surface-solid); border: 1px solid var(--border-strong); padding: 10px 16px; border-radius: 12px; z-index: 50; transition: all 0.2s;
  max-width: calc(100vw - 32px); box-shadow: var(--shadow);
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.err { border-color: rgba(248, 113, 113, 0.5); }
@media (max-width: 860px) { #toast { bottom: calc(var(--nav-h) + 16px + env(safe-area-inset-bottom)); } }

/* utilitários (sem estilos inline, por causa da CSP) */
.u-grow { flex: 1 1 160px; width: auto; }
.u-sm { font-size: 13px; }
.u-tight { gap: 4px; }
.u-flex1 { flex: 1; }
.u-mb { margin-bottom: 16px; }
.u-mt { margin-top: 12px; }
.u-mt0 { margin: 10px 0 0; }
.u-mt6 { margin: 6px 0 0; }
.u-link { text-decoration: none; color: inherit; display: block; }
main { min-width: 0; }
.bulkbar label.field { flex: 1 1 140px; min-width: 0; }
.bulkbar strong { flex: 1 0 100%; }
@media (min-width: 861px) { .bulkbar strong { flex: 0 0 auto; } }
.toolbar > * { min-width: 0; }

/* Acionamento (teste controlado) */
.ctl { border-top: 1px solid var(--border); padding-top: 14px; display: flex; flex-direction: column; gap: 12px; }
.ctl h3 { margin: 0; font-size: 16px; }
.ctl ul { padding-left: 18px; }
.ctl details summary { cursor: pointer; color: var(--muted); }
.ctl input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--accent); }
