/* SmartHouse — celular primeiro. Tema escuro por padrao; o claro vem de
   data-theme="light" no <html> (botao da topbar, salvo no localStorage). */
:root {
  --bg: #0b1120;
  --card: #131c2e;
  --border: #22304a;
  --text: #e5edf8;
  --muted: #8b9ab3;
  --primary: #3b82f6;
  --primary-press: #2563eb;
  --primary-soft: #1b2b4a;
  --warn: #f59e0b;
  --ok: #34d399;
  --ok-soft: #0f2e25;
  --err: #f87171;
  --err-soft: #3a1717;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 6px 20px rgba(0, 0, 0, .25);
  --radius: 16px;
  color-scheme: dark;
}
:root[data-theme="light"] {
  --bg: #f3f5f9;
  --card: #ffffff;
  --border: #e3e7ef;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #2563eb;
  --primary-press: #1d4ed8;
  --primary-soft: #dbe7ff;
  --warn: #d97706;
  --ok: #059669;
  --ok-soft: #d1fae5;
  --err: #dc2626;
  --err-soft: #fee2e2;
  --shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 4px 16px rgba(15, 23, 42, .06);
  color-scheme: light;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  min-height: 100vh;
}
h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1rem; }
h3 { font-size: 1.1rem; }
.muted { color: var(--muted); margin: .2rem 0 0; font-size: .9rem; }

.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; }
.brand svg { width: 22px; height: 22px; stroke: var(--primary); }
svg { fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

.badge {
  font-size: .75rem; padding: 3px 10px; border-radius: 999px;
  border: 1px solid var(--border); color: var(--muted);
}
.badge.online { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 40%, transparent); }
.badge.offline { color: var(--warn); }

main { max-width: 760px; margin: 0 auto; padding: 20px 16px 48px; }
.boot { text-align: center; color: var(--muted); padding: 60px 0; }

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

/* ---- login ---- */
.login { max-width: 400px; margin: 6vh auto 0; display: grid; gap: 14px; }
.field { display: grid; gap: 6px; font-size: .85rem; color: var(--muted); }
.field input, .field select {
  font: inherit; font-size: 16px; color: var(--text);
  background: var(--bg); border: 1px solid var(--border); border-radius: 12px;
  padding: 12px 14px; outline: none;
}
.field input:focus, .field select:focus { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
.msg { min-height: 1.2em; color: var(--err); font-size: .9rem; text-align: center; }

/* ---- botoes ---- */
.btn {
  font: inherit; font-weight: 600; cursor: pointer;
  border-radius: 12px; border: 1px solid transparent; padding: 10px 16px;
  transition: background .15s, transform .05s, box-shadow .15s;
  touch-action: manipulation;
}
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .6; cursor: default; }
.btn.primary { background: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-press); }
.btn.ghost { background: transparent; color: var(--text); border-color: var(--border); }
.btn.ghost:hover { background: var(--primary-soft); }
.btn.big { width: 100%; padding: 15px 16px; font-size: 1.05rem; }
.btn.small { padding: 5px 10px; font-size: .8rem; }

/* botao de acionar: normal / armado (confirmar) / enviando */
.btn .lbl-c, .btn .lbl-b { display: none; }
.btn.armed { background: var(--warn); animation: pulse 1s ease-in-out infinite; }
.btn.armed .lbl-n { display: none; }
.btn.armed .lbl-c { display: inline; }
.btn.busy .lbl-n, .btn.busy .lbl-c { display: none; }
.btn.busy .lbl-b { display: inline; }
@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--warn) 55%, transparent); }
  50% { box-shadow: 0 0 0 8px transparent; }
}

/* ---- painel ---- */
.home-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 18px; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 14px; }
.device {
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 18px; display: grid; gap: 12px;
}
.dev-top { display: flex; gap: 12px; align-items: center; }
.dev-icon {
  flex: none; width: 48px; height: 48px; border-radius: 14px;
  display: grid; place-items: center; background: var(--primary-soft); color: var(--primary);
}
.dev-icon svg { width: 28px; height: 28px; }
.dev-info {
  font-size: .82rem; color: var(--muted); padding: 7px 10px; border-radius: 10px;
  background: var(--bg);
}
.dev-info.on { color: var(--ok); background: var(--ok-soft); }
.dev-info.off { color: var(--err); background: var(--err-soft); }

.hist-card { margin-top: 18px; }
.card-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.hist { list-style: none; margin: 0; padding: 0; }
.hist li {
  display: grid; grid-template-columns: auto 1fr; column-gap: 12px;
  padding: 9px 0; border-top: 1px solid var(--border); font-size: .88rem;
}
.hist li:first-child { border-top: 0; }
.hist .h-when { color: var(--muted); font-variant-numeric: tabular-nums; grid-row: span 2; }
.hist .h-what { font-weight: 600; }
.hist .h-who { color: var(--muted); font-size: .8rem; }
.hist li.fail .h-what { color: var(--err); }
.hist li.empty { display: block; color: var(--muted); }

/* ---- toast ---- */
#toast-container {
  position: fixed; left: 50%; bottom: calc(18px + env(safe-area-inset-bottom));
  transform: translateX(-50%); display: grid; gap: 8px; z-index: 50;
  width: min(92vw, 420px);
}
.toast-item {
  padding: 12px 16px; border-radius: 12px; color: #fff; font-weight: 600;
  box-shadow: var(--shadow); background: #334155;
}
.toast-success { background: #059669; }
.toast-error { background: #dc2626; }
.toast-warning { background: #d97706; }

/* ---- tema ---- */
.top-right { display: flex; align-items: center; gap: 10px; }
.icon-btn {
  display: grid; place-items: center; width: 34px; height: 34px; padding: 0;
  border-radius: 10px; border: 1px solid var(--border); background: transparent;
  color: var(--muted); cursor: pointer;
}
.icon-btn:hover { color: var(--text); background: var(--primary-soft); }
.icon-btn svg { width: 18px; height: 18px; }
/* mostra o icone do tema para o qual o clique leva */
:root .ic-moon { display: none; }
:root[data-theme="light"] .ic-sun { display: none; }
:root[data-theme="light"] .ic-moon { display: block; }

/* ---- usuarios ---- */
.head-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.usr-layout { display: grid; gap: 14px; grid-template-columns: 1fr; }
@media (min-width: 760px) {
  main:has(#pnl-usuarios:not([style*="none"])) { max-width: 980px; }
  .usr-layout { grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr); align-items: start; }
}
.usr-form { display: grid; gap: 12px; }
.usr-form .card-head { margin-bottom: 0; }
.field-row { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); }
.field select { appearance: auto; }
.hint { margin: -4px 0 0; font-size: .8rem; color: var(--muted); }
.form-actions { display: flex; gap: 10px; flex-wrap: wrap; }
.form-actions .btn { flex: 1 1 140px; }
.btn.danger { background: transparent; color: var(--err); border-color: color-mix(in srgb, var(--err) 45%, transparent); }
.btn.danger:hover { background: var(--err-soft); }
.btn.danger.armed { background: var(--err); color: #fff; }
.msg.left { text-align: left; }
.msg.ok { color: var(--ok); }
.msg.err { color: var(--err); }
.small { font-size: .8rem; margin: 0; }
.search { margin-bottom: 6px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

.usr-list { list-style: none; margin: 0; padding: 0; }
.usr-row {
  display: grid; grid-template-columns: auto minmax(0, 1fr); align-items: center; gap: 4px 12px;
  padding: 10px 8px; border-top: 1px solid var(--border); cursor: pointer; border-radius: 10px;
}
.usr-row:first-child { border-top: 0; }
.usr-row:hover { background: var(--primary-soft); }
.usr-row.sel { background: var(--primary-soft); box-shadow: inset 3px 0 0 var(--primary); }
.avatar {
  grid-row: span 2; width: 38px; height: 38px; border-radius: 50%;
  display: grid; place-items: center; font-weight: 700;
  background: var(--primary-soft); color: var(--primary);
}
.usr-main { display: grid; min-width: 0; }
.usr-main strong, .usr-main small { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.usr-main small { color: var(--muted); }
.usr-tags { display: flex; gap: 6px; flex-wrap: wrap; }
.usr-tags:empty { display: none; }
.tag {
  font-size: .7rem; padding: 1px 8px; border-radius: 999px;
  border: 1px solid var(--border); color: var(--muted);
}
.tag.adm { color: var(--primary); border-color: color-mix(in srgb, var(--primary) 45%, transparent); }
.tag.off { color: var(--err); border-color: color-mix(in srgb, var(--err) 45%, transparent); }
.tag.me { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 45%, transparent); }