:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --text: #1d2330;
  --muted: #6b7280;
  --border: #e3e6ea;
  --accent: #1f6feb;
  --livre: #2e7d32;
  --moderado: #f9a825;
  --alto: #ef6c00;
  --critico: #c62828;
  --radius: 10px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11151c;
    --card: #1a202a;
    --text: #e6e9ef;
    --muted: #9aa3b2;
    --border: #2a313d;
    --accent: #58a6ff;
  }
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); }
[hidden] { display: none !important; }
h1 { margin: 0 0 4px; }
h2 { font-size: 0.95rem; margin: 0 0 10px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.muted { color: var(--muted); }
.small { font-size: .8rem; }
.error { color: var(--critico); min-height: 1.2em; }
.card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; }
button {
  background: var(--accent); color: #fff; border: 0; border-radius: 8px;
  padding: 8px 12px; font: inherit; cursor: pointer;
}
button.ghost { background: transparent; color: var(--text); border: 1px solid var(--border); }
button:disabled { opacity: .5; cursor: not-allowed; }
button[aria-pressed="true"] { outline: 2px solid var(--accent); outline-offset: 2px; }
input, select { font: inherit; padding: 6px 8px; border-radius: 6px; border: 1px solid var(--border); background: var(--card); color: var(--text); }
label { display: flex; flex-direction: column; gap: 4px; font-size: .85rem; }
fieldset { border: 0; padding: 0; margin: 8px 0; display: flex; gap: 12px; }
fieldset label { flex-direction: row; align-items: center; }
legend { font-size: .85rem; color: var(--muted); }

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(360px, 100%); display: flex; flex-direction: column; gap: 12px; }

/* App */
.app { height: 100vh; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 12px; padding: 8px 16px;
  background: var(--card); border-bottom: 1px solid var(--border); flex-wrap: wrap;
}
.brand { font-size: 1.05rem; }
.venue-picker { flex-direction: row; align-items: center; gap: 6px; }
.spacer { flex: 1; }
.status { font-size: .8rem; padding: 2px 8px; border-radius: 99px; background: var(--border); }
.status.on { background: color-mix(in srgb, var(--livre) 20%, transparent); color: var(--livre); }
.status.off { background: color-mix(in srgb, var(--critico) 18%, transparent); color: var(--critico); }

.layout { flex: 1; display: grid; grid-template-columns: 1fr 380px; min-height: 0; }
.map-wrap { position: relative; min-height: 0; }
#map { position: absolute; inset: 0; }
.side { overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 12px; border-left: 1px solid var(--border); }

.map-tools { position: absolute; top: 12px; left: 12px; width: 250px; display: flex; flex-direction: column; gap: 8px; }
.tool-buttons { display: flex; gap: 6px; }
.tool-buttons button { flex: 1; }
.legend { position: absolute; bottom: 24px; left: 12px; display: flex; gap: 12px; font-size: .8rem; padding: 8px 12px; flex-wrap: wrap; }
.cell-hover { position: absolute; bottom: 68px; left: 12px; font-size: .82rem; padding: 6px 10px; pointer-events: none; }
.sw { display: inline-block; width: 12px; height: 12px; border-radius: 3px; margin-right: 4px; vertical-align: -1px; }
.sw.livre { background: var(--livre); } .sw.moderado { background: var(--moderado); }
.sw.alto { background: var(--alto); } .sw.critico { background: var(--critico); }
.sw.dot { background: #0f172a; border: 2px solid #ffffff; border-radius: 50%; }

.route-result { font-size: .85rem; display: flex; flex-direction: column; gap: 4px; }
.route-result .rec { color: var(--livre); font-weight: 600; }

.big-number { font-size: 2.2rem; font-weight: 700; }
.big-number small { font-size: .9rem; font-weight: 400; color: var(--muted); }
.zone-cards { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 10px 0; }
.zone { border: 1px solid var(--border); border-left: 4px solid var(--border); border-radius: 8px; padding: 8px; }
.zone.livre { border-left-color: var(--livre); } .zone.moderado { border-left-color: var(--moderado); }
.zone.alto { border-left-color: var(--alto); } .zone.critico { border-left-color: var(--critico); }
.zone .name { font-size: .8rem; color: var(--muted); }
.zone .value { font-size: 1.2rem; font-weight: 600; }
.bar { height: 5px; border-radius: 3px; background: var(--border); margin-top: 6px; overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--accent); }
.row { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.row h2 { margin: 0; white-space: nowrap; }
.row select { min-width: 0; max-width: 60%; }
.chart-box { position: relative; height: 200px; }
#upload-form { display: flex; flex-direction: column; gap: 8px; }
.editor { max-height: calc(100% - 24px); overflow-y: auto; }
.editor h2 { margin: 0; }
.editor p { margin: 0; }
.editor .tool-buttons button { padding: 6px 8px; }

/* Abas */
.tabs { display: flex; gap: 4px; }
.tab { background: transparent; color: var(--text); border: 1px solid var(--border); padding: 5px 12px; }
.tab[aria-pressed="true"] { background: var(--accent); color: #fff; border-color: var(--accent); outline: none; }

/* Visão */
.vision-layout { flex: 1; display: grid; grid-template-columns: 380px 1fr; gap: 12px; padding: 12px; min-height: 0; overflow-y: auto; }
.vision-col { display: flex; flex-direction: column; gap: 12px; min-width: 0; }
.sensor-list { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.sensor-list button {
  width: 100%; text-align: left; background: transparent; color: var(--text);
  border: 1px solid var(--border); display: flex; justify-content: space-between; gap: 8px; align-items: center;
}
.sensor-list button[aria-pressed="true"] { border-color: var(--accent); outline: none; }
.sensor-list .meta { font-size: .78rem; color: var(--muted); }
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; background: var(--muted); flex: none; }
.dot.online, .dot.running { background: var(--livre); }
.dot.connecting, .dot.never_seen { background: var(--moderado); }
.dot.offline, .dot.error, .dot.no_model, .dot.over_budget { background: var(--critico); }
.sensor-form, .vt-form { display: flex; flex-direction: column; gap: 10px; }
.sensor-form h2 { margin: 0; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.sensor-form fieldset { flex-direction: column; gap: 4px; margin: 0; }
button.danger { color: var(--critico); border-color: color-mix(in srgb, var(--critico) 40%, transparent); }
.chips { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 8px; }
.chip { font-size: .75rem; padding: 2px 8px; border-radius: 99px; background: var(--border); display: inline-flex; gap: 5px; align-items: center; }
.info { display: grid; grid-template-columns: max-content 1fr; gap: 4px 12px; font-size: .85rem; margin: 0; }
.info dt { color: var(--muted); }
.info dd { margin: 0; overflow-wrap: anywhere; }
.publish ul { margin: 4px 0 0; padding-left: 18px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; overflow-wrap: anywhere; }
.key-box { margin-top: 10px; }
.frame-box { background: color-mix(in srgb, var(--border) 50%, transparent); border-radius: 8px; min-height: 120px; display: grid; place-items: center; overflow: hidden; }
.frame-box img, .frame-box video { max-width: 100%; max-height: 70vh; height: auto; display: block; }
#vt-video-box { display: flex; flex-direction: column; gap: 10px; margin-top: 10px; }
.stage {
  position: relative; width: 100%; margin: 0 auto; background: #000; border-radius: 8px; overflow: hidden;
  aspect-ratio: var(--ratio, 1.7778); max-width: calc(70vh * var(--ratio, 1.7778));
}
.stage video, .stage canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.stage canvas { pointer-events: none; }
.player-tools { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.player-tools button { padding: 5px 10px; }
.compact { align-items: center; }
.compact button { flex: none; padding: 5px 10px; }
label.inline { flex-direction: row; align-items: center; gap: 6px; white-space: nowrap; }
.sw.ok { background: #3cc83c; } .sw.weak { background: #ffc800; }
.sw.off { background: #ff00ff; }
/* Áreas ignoradas desenhadas sobre a imagem (telão, painel). */
.region-wrap { position: relative; display: inline-block; max-width: 100%; line-height: 0; }
.region-wrap canvas { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; }
.region-wrap canvas.drawing { cursor: crosshair; }
.region-wrap canvas:not(.drawing) { pointer-events: none; }
#vt-preview { display: flex; flex-direction: column; gap: 6px; }
#vt-preview .frame-box img, #vt-preview .frame-box video { max-height: 40vh; }

@media (max-width: 900px) {
  .vision-layout { grid-template-columns: 1fr; overflow: visible; }
  .layout { grid-template-columns: 1fr; grid-template-rows: 60vh auto; }
  .side { border-left: 0; overflow: visible; }
  .app { height: auto; min-height: 100vh; }
  .map-tools { width: calc(100% - 24px); }
}
