:root {
  --bg: #0e1014;
  --surface: #161920;
  --surface-2: #1d212a;
  --surface-3: #262b36;
  --line: #2b303b;
  --text: #e9ebef;
  --muted: #8d94a3;
  --amber: #f5b83d;
  --amber-glow: rgba(245, 184, 61, 0.45);
  --sky: #6ea8fe;
  --red: #ef6b6b;
  --green: #4cc38a;
  --radius: 14px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 var(--font);
  -webkit-tap-highlight-color: transparent;
  overflow: hidden;
}
[hidden] { display: none !important; }
h1, h2, h3 { margin: 0; font-weight: 600; letter-spacing: -0.01em; }
a { color: inherit; }
button, input, select { font: inherit; color: inherit; }

/* ------------------------------------------------------------ botões */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 40px; padding: 0 16px;
  background: var(--surface-3); border: 1px solid var(--line); border-radius: 10px;
  cursor: pointer; text-decoration: none; white-space: nowrap;
  transition: background .15s, border-color .15s, transform .08s;
}
.btn:hover { background: #2e3442; }
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .45; cursor: default; transform: none; }
.btn.primary { background: var(--amber); border-color: var(--amber); color: #1b1405; font-weight: 600; }
.btn.primary:hover { background: #ffc552; }
.btn.danger { background: transparent; border-color: #5a2b2b; color: var(--red); }
.btn.ghost { background: transparent; }
.btn.small { min-height: 32px; padding: 0 12px; font-size: 13px; border-radius: 8px; }
.btn.wide { width: 100%; min-height: 48px; }
.btn svg { width: 18px; height: 18px; flex: none; }

input:not([type=range]):not([type=file]), select {
  width: 100%; min-height: 40px; padding: 0 12px;
  background: var(--bg); border: 1px solid var(--line); border-radius: 10px;
}
input:focus-visible, select:focus-visible, .btn:focus-visible { outline: 2px solid var(--sky); outline-offset: 2px; }

/* ------------------------------------------------------------ login */
.login { height: 100%; display: grid; place-items: center; padding: 16px;
  background: radial-gradient(1200px 600px at 50% -10%, #2a2312 0%, var(--bg) 60%); }
.login-card { width: min(380px, 100%); background: var(--surface); border: 1px solid var(--line);
  border-radius: 20px; padding: 32px 28px; text-align: center; display: grid; gap: 14px; }
.login-card p { margin: 0; color: var(--muted); }
.login-error { color: var(--red) !important; background: #2a1717; border-radius: 10px; padding: 10px; }

.brand-mark { width: 22px; height: 22px; border-radius: 7px; flex: none;
  background: var(--amber); box-shadow: 0 0 18px var(--amber-glow);
  -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 3 2 11h3v9h5v-6h4v6h5v-9h3z'/%3E%3C/svg%3E") center/contain no-repeat;
          mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 3 2 11h3v9h5v-6h4v6h5v-9h3z'/%3E%3C/svg%3E") center/contain no-repeat; }
.brand-mark.big { width: 56px; height: 56px; margin: 0 auto 4px; }

/* ------------------------------------------------------------ shell */
.shell { height: 100%; display: flex; flex-direction: column; }
.topbar { display: flex; align-items: center; gap: 16px; padding: 10px 20px;
  border-bottom: 1px solid var(--line); background: var(--surface); flex: none; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; white-space: nowrap; }
.tabs { display: flex; gap: 4px; margin: 0 auto; overflow-x: auto; scrollbar-width: none; }
.tabs a { padding: 8px 14px; border-radius: 9px; text-decoration: none; color: var(--muted); white-space: nowrap; }
.tabs a:hover { color: var(--text); }
.tabs a.active { background: var(--surface-3); color: var(--text); }
.live { display: flex; align-items: center; gap: 7px; font-size: 12px; color: var(--muted); white-space: nowrap; }
.live .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.live.on .dot { background: var(--green); box-shadow: 0 0 8px var(--green); }
.live.off .dot { background: var(--red); }
.mock-banner { background: #2a2312; color: var(--amber); text-align: center; font-size: 13px; padding: 6px 16px; flex: none; }
.view { flex: 1; min-height: 0; overflow: auto; }

/* ------------------------------------------------------------ visão geral */
.overview { height: 100%; display: grid; grid-template-columns: minmax(0, 1fr) 340px; }
.side { border-left: 1px solid var(--line); background: var(--surface); overflow-y: auto; padding: 20px; display: grid; gap: 24px; align-content: start; }
.side h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin-bottom: 10px; }

.stage { position: relative; min-height: 0; min-width: 0; padding: 20px; overflow: hidden; }
.plan { position: absolute; border-radius: var(--radius); background: var(--surface) center/100% 100% no-repeat; touch-action: none;
  box-shadow: 0 10px 40px rgba(0,0,0,.35); }
.plan.dim::after { content: ""; position: absolute; inset: 0; border-radius: inherit; background: rgba(8,9,12,.28); pointer-events: none; }
.empty { position: absolute; inset: 20px; display: grid; place-items: center; text-align: center; color: var(--muted);
  border: 1.5px dashed var(--line); border-radius: var(--radius); padding: 24px; }
.empty > div { display: grid; gap: 12px; justify-items: center; max-width: 360px; }

/* marcadores */
.marker { position: absolute; transform: translate(-50%, -50%); display: grid; justify-items: center; gap: 4px;
  z-index: 2; cursor: pointer; user-select: none; -webkit-user-select: none; }
.marker .knob { width: 46px; height: 46px; border-radius: 50%; display: grid; place-items: center; position: relative;
  background: rgba(32, 36, 46, .92); border: 2px solid #4a5160; color: #a9b0bd;
  backdrop-filter: blur(4px); transition: background .2s, box-shadow .25s, border-color .2s, color .2s, transform .1s; }
.marker .knob svg { width: 22px; height: 22px; }
.marker:active .knob { transform: scale(.92); }
.marker .tag { font-size: 11px; line-height: 1; padding: 4px 7px; border-radius: 6px; background: rgba(14,16,20,.82);
  color: #cfd3db; white-space: nowrap; max-width: 140px; overflow: hidden; text-overflow: ellipsis; pointer-events: none; }
.marker.on .knob { background: var(--amber); border-color: #ffd47e; color: #2a1d00; box-shadow: 0 0 0 6px rgba(245,184,61,.18), 0 0 26px var(--amber-glow); }
.marker.k-curtain .knob { border-color: var(--sky); color: var(--sky); }
.marker .pct { position: absolute; bottom: -6px; right: -10px; font-size: 10px; font-weight: 700; padding: 2px 5px; border-radius: 6px;
  background: var(--sky); color: #08172e; }
.marker.offline .knob { border-style: dashed; border-color: var(--red); color: var(--red); opacity: .75; box-shadow: none; background: rgba(32,36,46,.92); }
.marker.missing .knob { border-style: dashed; opacity: .5; }
.marker.busy .knob::after { content: ""; position: absolute; inset: -6px; border-radius: 50%; border: 2px solid transparent;
  border-top-color: currentColor; animation: spin .8s linear infinite; }
.marker.editing { cursor: grab; }
.marker.editing.dragging { cursor: grabbing; z-index: 5; }
.marker.editing.dragging .knob { transform: scale(1.12); box-shadow: 0 8px 24px rgba(0,0,0,.5); }
@keyframes spin { to { transform: rotate(360deg); } }

/* popover da persiana */
.popover { position: absolute; z-index: 10; width: 250px; background: var(--surface-2); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 16px; box-shadow: 0 16px 40px rgba(0,0,0,.5); display: grid; gap: 12px; }
.popover header { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.popover header strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.popover .value { color: var(--sky); font-variant-numeric: tabular-nums; font-size: 13px; }
.popover .btns { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.popover .btns .btn { padding: 0 6px; }
input[type=range] { width: 100%; accent-color: var(--sky); height: 28px; }

/* painel lateral */
.room-list, .scene-grid { display: grid; gap: 8px; }
.room { background: var(--surface-2); border: 1px solid var(--line); border-radius: 12px; }
.room-head { display: flex; align-items: center; gap: 12px; padding: 12px 14px; cursor: pointer; }
.room-head .meta { flex: 1; min-width: 0; }
.room-head .name { font-weight: 600; }
.room-head .count { font-size: 13px; color: var(--muted); }
.room-head .count b { color: var(--amber); font-weight: 600; }
.room .bulb-ic { width: 34px; height: 34px; border-radius: 10px; display: grid; place-items: center; background: var(--surface-3); color: var(--muted); flex: none; }
.room .bulb-ic svg { width: 18px; height: 18px; }
.room.lit .bulb-ic { background: rgba(245,184,61,.16); color: var(--amber); }
.room-body { border-top: 1px solid var(--line); padding: 6px; display: grid; gap: 2px; }
.dev-row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 10px; padding: 8px; border-radius: 9px; }
.dev-row:hover { background: var(--surface-3); }
.dev-row .label { flex: 1 1 120px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dev-row .sub { font-size: 12px; color: var(--muted); }

.switch { position: relative; width: 46px; height: 28px; flex: none; border-radius: 14px; border: none; padding: 0; cursor: pointer;
  background: var(--surface-3); transition: background .2s; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #c6cbd4; transition: transform .2s, background .2s; }
.switch[aria-checked=true] { background: var(--amber); }
.switch[aria-checked=true]::after { transform: translateX(18px); background: #fff; }
.switch:disabled { opacity: .4; cursor: default; }

.scene-grid { grid-template-columns: repeat(auto-fill, minmax(92px, 1fr)); }
.scene { display: grid; justify-items: center; gap: 8px; padding: 14px 8px; border-radius: 12px; cursor: pointer;
  background: var(--surface-2); border: 1px solid var(--line); transition: background .15s, border-color .15s, transform .08s; }
.scene:hover { border-color: #3c4352; }
.scene:active { transform: scale(.96); }
.scene svg { width: 26px; height: 26px; color: var(--amber); }
.scene span { font-size: 13px; }
.scene.running { border-color: var(--amber); background: rgba(245,184,61,.1); }
.hint { color: var(--muted); font-size: 13px; margin: 0; }
.hint a { color: var(--amber); }

/* ------------------------------------------------------------ editor */
.editor { height: 100%; display: flex; flex-direction: column; }
.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; padding: 12px 20px; border-bottom: 1px solid var(--line); background: var(--surface); }
.toolbar .spacer { flex: 1; }
.toolbar .status { font-size: 13px; color: var(--muted); }
.toolbar .status.dirty { color: var(--amber); }
.editor .stage { flex: 1; }
.plan.editing { outline: 2px dashed rgba(110,168,254,.35); outline-offset: 4px; }

/* ------------------------------------------------------------ páginas (cômodos / config) */
.page { max-width: 1100px; margin: 0 auto; padding: 24px 20px 48px; display: grid; gap: 24px; }
.page-head { display: flex; flex-wrap: wrap; align-items: end; justify-content: space-between; gap: 12px; }
.page-head h1 { font-size: 24px; }
.page-head p { margin: 4px 0 0; color: var(--muted); }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 14px; }
.card { min-width: 0; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; display: grid; gap: 12px; align-content: start; }
.card-head { display: flex; align-items: center; gap: 8px; }
.card-head input { font-weight: 600; background: transparent; border-color: transparent; padding: 0 8px; margin-left: -8px; }
.card-head input:hover, .card-head input:focus { border-color: var(--line); background: var(--bg); }
.inline-form { display: flex; gap: 8px; }
.inline-form input { flex: 1; }
.section { display: grid; gap: 12px; }
.section > h2 { font-size: 17px; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; font-size: 14px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
.badge { display: inline-block; font-size: 11px; padding: 2px 7px; border-radius: 6px; background: var(--surface-3); color: var(--muted); vertical-align: middle; }
.badge.ok { background: rgba(76,195,138,.15); color: var(--green); }
.badge.bad { background: rgba(239,107,107,.15); color: var(--red); }

.scene-edit { display: grid; gap: 10px; }
.scene-edit .top { display: grid; grid-template-columns: 1fr 140px; gap: 8px; }
.action-row { display: grid; grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr) 40px; gap: 6px; align-items: center; }
.icon-btn { width: 40px; height: 40px; display: grid; place-items: center; border-radius: 10px; border: 1px solid var(--line); background: transparent; color: var(--muted); cursor: pointer; }
.icon-btn:hover { color: var(--red); border-color: #5a2b2b; }
.icon-btn svg { width: 16px; height: 16px; }
.btn-row { display: flex; flex-wrap: wrap; gap: 8px; }

details.diag { background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px; padding: 10px 14px; }
details.diag summary { cursor: pointer; display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
details.diag pre { margin: 10px 0 0; font-size: 12px; overflow: auto; color: #b9c0cc; background: var(--bg); padding: 10px; border-radius: 8px; }
code { font-size: 12px; color: #b9c0cc; }

/* ------------------------------------------------------------ dialog */
.dialog { width: min(520px, calc(100% - 32px)); background: var(--surface-2); color: var(--text); border: 1px solid var(--line);
  border-radius: 18px; padding: 0; box-shadow: 0 24px 60px rgba(0,0,0,.6); }
.dialog::backdrop { background: rgba(4,5,8,.6); backdrop-filter: blur(2px); }
.dialog form { padding: 22px; display: grid; gap: 14px; }
.dialog label { display: grid; gap: 6px; font-size: 13px; color: var(--muted); }
.row2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }
.dialog-actions { display: flex; gap: 8px; margin-top: 6px; }
.dialog-actions .spacer { flex: 1; }

/* ------------------------------------------------------------ toasts */
.toasts { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); display: grid; gap: 8px; z-index: 100; width: min(440px, calc(100% - 32px)); }
.toast { background: var(--surface-3); border: 1px solid var(--line); border-left: 3px solid var(--sky); border-radius: 10px; padding: 12px 14px;
  box-shadow: 0 10px 30px rgba(0,0,0,.4); animation: rise .2s ease-out; }
.toast.error { border-left-color: var(--red); }
.toast.ok { border-left-color: var(--green); }
@keyframes rise { from { opacity: 0; transform: translateY(8px); } }

/* ------------------------------------------------------------ responsivo */
@media (max-width: 900px) {
  body { overflow: auto; }
  .overview { grid-template-columns: 1fr; grid-template-rows: auto auto; height: auto; }
  .overview .stage { height: 62vh; min-height: 320px; }
  .side { border-left: none; border-top: 1px solid var(--line); }
  .shell { height: auto; min-height: 100%; }
  .view { overflow: visible; }
  .editor { height: auto; }
  .editor .stage { height: 70vh; min-height: 320px; flex: none; }
}
@media (max-width: 640px) {
  .topbar { flex-wrap: wrap; padding: 10px 16px; gap: 8px; }
  .tabs { order: 3; width: 100%; margin: 0; }
  .live { margin-left: auto; }
  .stage { padding: 12px; }
  .empty { inset: 12px; }
  .side { padding: 16px; }
  .page { padding: 20px 16px 40px; }
  .toolbar { padding: 10px 16px; }
  .marker .knob { width: 40px; height: 40px; }
  .marker .tag { display: none; }
  .cards { grid-template-columns: 1fr; }
}

/* ------------------------------------------------------------ visualizador 3D */
.scene3d { position: absolute; inset: 20px; border-radius: var(--radius); overflow: hidden;
  background: radial-gradient(120% 90% at 50% 15%, #2a2f3a 0%, #171a21 55%, #101216 100%); box-shadow: 0 10px 40px rgba(0,0,0,.35); }
.scene3d canvas { display: block; width: 100%; height: 100%; touch-action: none; outline: none; }
.scene3d.picking canvas { cursor: crosshair; }
.scene3d.picking { box-shadow: 0 0 0 2px var(--sky), 0 10px 40px rgba(0,0,0,.35); }
.scene3d-layer { position: absolute; inset: 0; pointer-events: none; }
.scene3d-layer .marker { pointer-events: auto; transition: opacity .2s; }
.scene3d-layer .marker.occluded { opacity: .38; }
.scene3d-layer .marker.occluded .tag { display: none; }
.scene3d-loading { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); padding: 24px; text-align: center; }
.scene3d-tools { position: absolute; left: 12px; bottom: 12px; display: flex; gap: 6px; }
.scene3d-tools .btn { background: rgba(29,33,42,.85); backdrop-filter: blur(6px); }

.seg { display: inline-flex; padding: 3px; gap: 2px; border-radius: 10px; background: var(--bg); border: 1px solid var(--line); }
.seg button { border: 0; background: transparent; color: var(--muted); padding: 0 12px; min-height: 32px; border-radius: 7px; cursor: pointer; font-size: 13px; white-space: nowrap; }
.seg button[aria-pressed=true] { background: var(--surface-3); color: var(--text); }
.stage-seg { position: absolute; top: 30px; right: 30px; z-index: 6; background: rgba(14,16,20,.85); backdrop-filter: blur(6px); }
.toolbar .sep { width: 1px; height: 24px; background: var(--line); }
.toolbar .tools-2d, .toolbar .tools-3d { display: flex; flex-wrap: wrap; gap: 8px; }
.toolbar .default-view { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); }
.toolbar .default-view select { min-height: 34px; font-size: 13px; }
@media (max-width: 640px) {
  .scene3d { inset: 12px; }
  .stage-seg { top: 20px; right: 20px; }
  .toolbar .sep { display: none; }
}
