:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --text: #1c2430;
  --muted: #667085;
  --border: #dde2ea;
  --accent: #0e7c86;
  --accent-text: #ffffff;
  --ok: #1a7f37;
  --ok-bg: #e6f4ea;
  --warn: #9a6700;
  --warn-bg: #fff4d6;
  --alarm: #c62828;
  --alarm-bg: #fdecea;
  --off: #667085;
  --off-bg: #eef0f3;
  --temp: #e8590c;
  --hum: #1971c2;
  --radius: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12161c;
    --surface: #1b2129;
    --text: #e6e9ee;
    --muted: #98a2b3;
    --border: #2c3440;
    --accent: #2bb3bf;
    --accent-text: #0b1215;
    --ok: #4ac26b;
    --ok-bg: #16301f;
    --warn: #e3b341;
    --warn-bg: #3a2e10;
    --alarm: #ff6b6b;
    --alarm-bg: #3b1a1a;
    --off: #98a2b3;
    --off-bg: #262d37;
    --temp: #ff8c42;
    --hum: #4dabf7;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
/* O atributo hidden precisa vencer regras como display:grid/inline-block dos formulários e botões */
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
code { font: 0.9em ui-monospace, SFMono-Regular, Consolas, monospace; background: var(--off-bg); padding: 1px 5px; border-radius: 4px; }
h1 { font-size: 1.5rem; margin: 0; }
h2 { font-size: 1.05rem; margin: 0 0 12px; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }

/* Barra superior */
.topbar { background: var(--surface); border-bottom: 1px solid var(--border); }
.topbar-inner { max-width: 1200px; margin: 0 auto; padding: 10px 16px; display: flex; align-items: center; gap: 16px; flex-wrap: wrap; }
.brand { display: flex; line-height: 0; }
.brand img { height: 58px; width: auto; }
.login-logo { margin: 0 0 4px; line-height: 0; text-align: center; }
.login-logo img { width: 100%; max-width: 300px; height: auto; }
/* O logo tem fundo branco: no tema escuro fica sobre um "cartão" claro */
@media (prefers-color-scheme: dark) {
  .brand img, .login-logo img { background: #fff; border-radius: 8px; padding: 3px 6px; }
}
nav { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; margin-left: auto; }
nav a { color: var(--muted); padding: 6px 10px; border-radius: 6px; }
nav a.on, nav a:hover { color: var(--text); background: var(--off-bg); text-decoration: none; }
form.inline { display: inline; margin: 0; }
button.link { background: none; border: 0; color: var(--muted); padding: 6px 10px; cursor: pointer; font: inherit; }
button.link:hover { color: var(--text); }

.container { max-width: 1200px; margin: 0 auto; padding: 24px 16px 48px; }
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.page-head p { margin: 4px 0 0; }
.back { font-size: 0.9rem; display: inline-block; margin-bottom: 4px; }
.count { font-size: 1rem; font-weight: 400; }

/* Botões e campos */
button, .button {
  display: inline-block;
  font: inherit;
  padding: 8px 14px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  text-decoration: none;
}
button:hover, .button:hover { border-color: var(--muted); text-decoration: none; }
.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.primary:hover { filter: brightness(1.08); border-color: var(--accent); }
.danger { color: var(--alarm); border-color: var(--alarm); }
input, select, textarea {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
input[type="checkbox"] { width: auto; }
input:disabled { color: var(--muted); background: var(--off-bg); }
label { display: block; font-weight: 500; }
label input, label select, label textarea { margin-top: 4px; font-weight: 400; }
.stack { display: grid; gap: 14px; }

.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; margin-bottom: 16px; }
.narrow { max-width: 460px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.span-2 { grid-column: 1 / -1; }
fieldset { border: 1px solid var(--border); border-radius: 8px; padding: 12px 14px; margin: 0; }
legend { font-weight: 600; padding: 0 6px; }
fieldset > p { margin: 0 0 8px; }
.limits { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
.checks { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 6px 16px; max-height: 320px; overflow: auto; margin-top: 8px; }
label.check { display: flex; align-items: center; gap: 8px; font-weight: 400; }
fieldset input[type="search"] { max-width: 320px; }
.actions { display: flex; gap: 10px; flex-wrap: wrap; }
.danger-zone .actions form { margin: 0; }

.alert { padding: 10px 14px; border-radius: 8px; margin-bottom: 16px; border: 1px solid transparent; }
.alert ul { margin: 0; padding-left: 18px; }
.alert.ok { background: var(--ok-bg); color: var(--ok); }
.alert.error { background: var(--alarm-bg); color: var(--alarm); }
.alert.warn { background: var(--warn-bg); color: var(--warn); }
.alert.info { background: var(--off-bg); color: var(--text); }

/* Login */
.login { max-width: 360px; margin: 10vh auto 0; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 28px; }
.login h1 { font-size: 1.6rem; }
.login p { margin: 4px 0 20px; }
.login h2 { margin: 16px 0 8px; }
.login p.login-links { margin: 16px 0 0; text-align: center; font-size: 0.9rem; }

/* Painel */
.toolbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.toolbar input[type="search"] { max-width: 340px; }
.summary { display: flex; gap: 8px; flex-wrap: wrap; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 14px; }
.card {
  display: block;
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--off);
  border-radius: var(--radius);
  padding: 14px 16px;
  color: var(--text);
}
.card:hover { text-decoration: none; border-color: var(--muted); }
.card.ok { border-left-color: var(--ok); }
.card.alarm { border-left-color: var(--alarm); background: linear-gradient(var(--alarm-bg), var(--surface) 60%); }
.card-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.card .name { font-weight: 600; overflow-wrap: anywhere; }
.card .loc { color: var(--muted); font-size: 0.85rem; min-height: 1.3em; }
.card .values { display: flex; gap: 18px; margin: 10px 0 4px; }
.card .v { font-size: 1.7rem; font-weight: 600; font-variant-numeric: tabular-nums; }
.card .v.t { color: var(--temp); }
.card .v.h { color: var(--hum); }
.card .u { color: var(--muted); margin-left: 2px; }
.card .seen { color: var(--muted); font-size: 0.8rem; }
.empty-state { text-align: center; padding: 48px 16px; }

.badge { display: inline-block; font-size: 0.75rem; font-weight: 600; padding: 2px 8px; border-radius: 999px; white-space: nowrap; }
.badge.ok { background: var(--ok-bg); color: var(--ok); }
.badge.alarm { background: var(--alarm-bg); color: var(--alarm); }
.badge.off { background: var(--off-bg); color: var(--off); }
.badge.pending { background: var(--warn-bg); color: var(--warn); }
.card.pending { border-left-color: var(--warn); border-style: dashed; border-left-style: solid; }
.card .activate-hint { color: var(--muted); font-size: 0.85rem; margin: 10px 0; }
.claim { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px 24px; padding: 16px 20px; }
.claim h2 { margin: 0; }
.claim p { margin: 4px 0 0; }
.claim .inline-form { margin: 0; flex: 1 1 320px; max-width: 460px; }
.claim .alert { flex-basis: 100%; margin: 0; }

/* Detalhe do dispositivo */
.current { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; margin-bottom: 16px; }
.metric { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.metric .label { display: block; color: var(--muted); font-size: 0.85rem; }
.metric .value { font-size: 2rem; font-weight: 600; font-variant-numeric: tabular-nums; }
.metric .unit { color: var(--muted); margin-left: 3px; }
.metric .status { display: block; margin: 6px 0 4px; }
.metric .small { display: block; }
#cur-temp { color: var(--temp); }
#cur-hum { color: var(--hum); }
.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.segmented button { border: 0; border-radius: 0; border-right: 1px solid var(--border); }
.segmented button:last-child { border-right: 0; }
.segmented button.on { background: var(--accent); color: var(--accent-text); font-weight: 600; }
.chart-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; margin-bottom: 16px; }
.chart-box { position: relative; height: 280px; }

/* Tabelas */
.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.03em; color: var(--muted); font-weight: 600; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: var(--off-bg); }
.num { text-align: right; font-variant-numeric: tabular-nums; }
td.out { color: var(--alarm); font-weight: 600; }
td.out-range { color: var(--warn); font-weight: 600; }
.badge.warn { background: var(--warn-bg); color: var(--warn); }
.legend { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 10px; margin: -4px 0 12px; color: var(--muted); }
.legend .badge + .badge { margin-left: 10px; }

/* Tabela de leituras */
.filters { justify-content: flex-start; align-items: flex-end; gap: 16px; }
.filters select { width: auto; min-width: 180px; }
.filters label.check { padding-bottom: 8px; }
.filters .push-right { margin-left: auto; }
.load-more { text-align: center; margin-top: 16px; }
.short { display: none; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
h2.section { margin: 28px 0 12px; }
.section-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin: 28px 0 12px; }
.section-head h2 { margin: 0; }
.row-actions { display: flex; gap: 6px; align-items: center; }
.row-actions form { margin: 0; }
.edit-location { margin-top: 6px; font-size: 0.9rem; }
.edit-location summary { color: var(--accent); cursor: pointer; width: max-content; }
.inline-form { display: flex; gap: 8px; margin-top: 8px; max-width: 460px; }
.inline-form input { margin: 0; }
.manage h2 { margin-bottom: 4px; }
.manage-row { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; padding: 14px 0; border-top: 1px solid var(--border); margin: 0; }
.manage-row:first-of-type { border-top: 0; }
.manage-row p { margin: 4px 0 0; }
.delete-fields { flex: 1; display: grid; gap: 10px; }
.delete-fields .limits { grid-template-columns: repeat(2, minmax(0, 240px)); }
.row-delete { border: 0; background: none; color: var(--muted); padding: 2px 6px; font-size: 1.1rem; line-height: 1; }
.row-delete:hover { color: var(--alarm); }
.narrow-wide { max-width: 640px; }
.plan-note { margin-top: 4px; }

/* Período de acesso */
.access-banner { background: var(--warn-bg); color: var(--warn); border-bottom: 1px solid var(--border); }
.access-banner.urgent { background: var(--alarm-bg); color: var(--alarm); }
.access-banner-inner { max-width: 1200px; margin: 0 auto; padding: 8px 16px; display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.access-banner .button { padding: 5px 12px; }
.access-line { margin: 2px 0 0; }
.wide-login { max-width: 460px; }
.plan-list { margin: 0 0 16px; padding-left: 20px; }
.plan-list li { margin-bottom: 6px; }
.button.block { display: block; text-align: center; }
.rules-title { font-size: 0.95rem; margin: 20px 0 8px; }
.big-count { font-size: 1.4rem; color: var(--alarm); }
td.wrap { white-space: normal; min-width: 220px; }

.creds { display: grid; grid-template-columns: max-content 1fr; gap: 10px 20px; margin: 0; }
.creds dt { color: var(--muted); }
.creds dd { margin: 0; overflow-wrap: anywhere; }
.creds .secret { font-size: 1.05em; font-weight: 600; }

@media (max-width: 720px) {
  .form-grid { grid-template-columns: 1fr; }
  .limits { grid-template-columns: 1fr 1fr; }
  .current { grid-template-columns: 1fr 1fr; }
  .current .metric:last-child { grid-column: 1 / -1; }
  .chart-box { height: 220px; }
  .toolbar input[type="search"] { max-width: none; }
  .brand img { height: 44px; }
  .filters label:not(.check) { flex: 1 1 140px; }
  .filters select { width: 100%; min-width: 0; }
  /* No celular: sem Local e Situação (fora da faixa já aparece em vermelho) */
  #readings th:nth-child(3), #readings td:nth-child(3),
  #readings th:nth-child(6), #readings td:nth-child(6) { display: none; }
  #readings th, #readings td { padding: 8px 6px; }
  #readings td:nth-child(1), #readings td:nth-child(2) { white-space: normal; }
  .long { display: none; }
  .short { display: inline; }
  .manage-row { flex-direction: column; align-items: stretch; }
  .delete-fields .limits { grid-template-columns: 1fr; }
  .creds { grid-template-columns: 1fr; gap: 2px; }
  .creds dd { margin-bottom: 10px; }
}
