:root {
  --bg: #f6f7f9; --card: #fff; --text: #1d2128; --muted: #6b7280; --line: #e5e7eb;
  --on: #16a34a; --on-bg: #dcfce7; --off: #6b7280; --off-bg: #f1f2f4;
  --bad: #dc2626; --bad-bg: #fee2e2; --accent: #2563eb; --accent-bg: #dbeafe;
  --warn: #b45309; --warn-bg: #fef3c7; --shade: rgba(15, 20, 30, .45);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111418; --card: #1a1e24; --text: #e6e8eb; --muted: #9aa1ab; --line: #2a3038;
    --on: #4ade80; --on-bg: #14361f; --off: #9aa1ab; --off-bg: #262b33;
    --bad: #f87171; --bad-bg: #3b1717; --accent: #60a5fa; --accent-bg: #1e3050;
    --warn: #fbbf24; --warn-bg: #3a2a0c; --shade: rgba(0, 0, 0, .6);
  }
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text);
       font: 14px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; }
header { display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
         padding: 12px 16px; background: var(--card); border-bottom: 1px solid var(--line);
         position: sticky; top: 0; z-index: 2; }
h1 { font-size: 17px; margin: 0; }
select, input, textarea { font: inherit; color: var(--text); background: var(--bg);
                border: 1px solid var(--line); border-radius: 6px; padding: 5px 8px; }
#engine { margin-left: auto; font-size: 13px; padding: 3px 10px; border-radius: 99px; }
#engine.ok { background: var(--on-bg); color: var(--on); }
#engine.ko { background: var(--bad-bg); color: var(--bad); }
main { padding: 16px; max-width: 1240px; margin: 0 auto; }
.bar { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; }
.chip { border: 1px solid var(--line); background: var(--card); color: var(--text);
        border-radius: 99px; padding: 4px 12px; cursor: pointer; font: inherit; }
.chip.sel { background: var(--accent-bg); border-color: var(--accent); color: var(--accent); }
.chip small { color: var(--muted); margin-left: 4px; }
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 6px;
       vertical-align: 0; }
#search { margin-left: auto; min-width: 200px; }
h2 { font-size: 15px; margin: 24px 0 8px; }
.wrap { overflow-x: auto; background: var(--card); border: 1px solid var(--line);
        border-radius: 8px; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 7px 10px; border-bottom: 1px solid var(--line);
         white-space: nowrap; }
th { font-size: 12px; color: var(--muted); font-weight: 600; }
tr:last-child td { border-bottom: none; }
td.id { color: var(--muted); font-size: 12px; font-family: ui-monospace, Menlo, monospace; }
td.vals { white-space: normal; min-width: 260px; color: var(--muted); font-size: 13px; }
td.dim, td.dim b { color: var(--muted); opacity: .6; font-weight: 400; }
td.place { cursor: pointer; }
td.place:hover { background: var(--accent-bg); }
.pill { display: inline-block; min-width: 44px; text-align: center; padding: 2px 8px;
        border-radius: 99px; font-size: 12px; font-weight: 600; }
.pill.on { background: var(--on-bg); color: var(--on); }
.pill.off { background: var(--off-bg); color: var(--off); }
.pill.bad { background: var(--bad-bg); color: var(--bad); }
.pill.unk { background: var(--off-bg); color: var(--muted); font-weight: 400; }
.pill.warn { background: var(--warn-bg); color: var(--warn); font-weight: 400; }
.pill.tag { font-weight: 500; min-width: 0; }
.pill.sys { font-weight: 400; min-width: 0; font-style: italic; background: transparent;
            border: 1px dashed var(--line); color: var(--muted); }
tr.offline td:not(.st):not(.place) { opacity: .55; }
tr.fresh td { background: var(--accent-bg); transition: background 2s; }
.num { font-variant-numeric: tabular-nums; }
.empty { padding: 16px; color: var(--muted); }
.act { border: none; background: none; color: var(--accent); cursor: pointer; font: inherit;
       padding: 0 6px; }
.act:hover { text-decoration: underline; }
/* navigazione */
.nav { display: flex; gap: 4px; }
.nav button { border: none; background: none; color: var(--muted); font: inherit; font-weight: 600;
              padding: 6px 12px; border-radius: 6px; cursor: pointer; }
.nav button.sel { background: var(--accent-bg); color: var(--accent); }
section[hidden] { display: none; }
/* Casa: riquadri delle stanze in stile Metro */
#homes { margin-bottom: 14px; }
.rooms { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
         grid-auto-flow: dense; gap: 12px; align-items: start; }
.room { background: var(--card); border: 1px solid var(--line); grid-column: span 1; }
.room.m { grid-column: span 2; }
.room.l { grid-column: span 3; }
@media (max-width: 700px) { .room.m, .room.l { grid-column: span 1; } }
.room h3 { margin: 0; padding: 10px 12px; font-size: 15px; font-weight: 600; color: #fff;
           background: var(--rc); }
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: 6px;
         padding: 8px; }
.tile { position: relative; aspect-ratio: 1; display: flex; flex-direction: column; justify-content: space-between;
        padding: 8px; background: var(--off-bg); color: var(--text); cursor: default;
        overflow: hidden; }
.tile.edit { cursor: pointer; }
.tile.on { background: var(--rc); color: #fff; }
.tile.offline { opacity: .4; }
.tile .tname { font-size: 13px; font-weight: 600; line-height: 1.25; word-break: break-word;
               display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; }
.tile .tval { font-size: 12px; opacity: .85; }
.tile .tbig { font-size: 26px; font-weight: 300; line-height: 1; }
.room.pending h3 { background: #b45309; }
.room.vacant { border: none; background: none; }
.wx { display: inline-flex; align-items: center; gap: 6px; margin-left: 8px; font-size: 18px;
      font-weight: 300; color: var(--text); }
.wx svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8;
          stroke-linecap: round; stroke-linejoin: round; }
.ticon { position: absolute; right: 8px; bottom: 8px; width: 22px; height: 22px; fill: none;
         stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tile:not(.on) .ticon { color: var(--muted); }
.icons { display: flex; flex-wrap: wrap; gap: 6px; }
.icons label { display: flex; flex-direction: column; align-items: center; gap: 2px; cursor: pointer;
               padding: 6px; border: 1px solid var(--line); border-radius: 8px; min-width: 64px;
               font-size: 11px; color: var(--muted); }
.icons input { display: none; }
.icons label:has(input:checked) { border-color: var(--accent); background: var(--accent-bg);
                                  color: var(--accent); }
.icons svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8;
             stroke-linecap: round; stroke-linejoin: round; color: var(--text); }
.attrs { width: 100%; font-size: 13px; }
.attrs td { padding: 3px 8px 3px 0; border: none; white-space: normal; vertical-align: top; }
.attrs td:first-child { font-family: ui-monospace, Menlo, monospace; white-space: nowrap;
                        width: 1%; padding-right: 18px; }
/* finestre */
.modal { position: fixed; inset: 0; background: var(--shade); display: flex;
         align-items: flex-start; justify-content: center; padding: 6vh 16px; z-index: 10; }
.modal[hidden] { display: none; }
.box { background: var(--card); border: 1px solid var(--line); border-radius: 10px;
       width: 100%; max-width: 560px; max-height: 88vh; overflow: auto; padding: 18px; }
.box.wide { max-width: 900px; }
.box h3 { margin: 0 0 4px; font-size: 16px; }
.box .sub { color: var(--muted); font-size: 12px; font-family: ui-monospace, Menlo, monospace;
            margin-bottom: 14px; word-break: break-all; }
.box fieldset { border: none; padding: 0; margin: 0 0 14px; }
.box legend { font-size: 12px; font-weight: 600; color: var(--muted); margin-bottom: 6px;
              text-transform: uppercase; letter-spacing: .04em; }
.box label.opt { display: flex; align-items: center; gap: 8px; padding: 3px 0; cursor: pointer; }
.box label.opt small { color: var(--muted); }
.box input[type=text] { width: 100%; }
.box .reset { margin-top: 6px; }
.edwrap { display: flex; gap: 10px; align-items: stretch; }
.edwrap textarea { flex: 1; }
#ed-names[hidden] { display: none; }
#ed-names { width: 230px; flex: none; max-height: 60vh; overflow: auto; font-size: 12px;
            border: 1px solid var(--line); border-radius: 6px; padding: 8px; }
#ed-names .nh { font-weight: 600; margin-bottom: 6px; }
#ed-names .ns { color: var(--muted); text-transform: uppercase; letter-spacing: .04em;
                font-size: 10px; margin: 8px 0 3px; }
#ed-names .nm { display: flex; flex-direction: column; padding: 3px 4px; border-radius: 4px;
                cursor: pointer; font-family: ui-monospace, Menlo, monospace; }
#ed-names .nm:hover { background: var(--accent-bg); }
#ed-names .nm span { color: var(--muted); font-size: 11px; word-break: break-all; }
@media (max-width: 700px) { .edwrap { flex-direction: column; } #ed-names { width: auto; } }
.box textarea { width: 100%; min-height: 52vh; resize: vertical; white-space: pre;
                font: 13px/1.5 ui-monospace, Menlo, monospace; tab-size: 4; }
.row { display: flex; gap: 8px; align-items: center; }
.row .ext { color: var(--muted); font-family: ui-monospace, Menlo, monospace; }
.buttons { display: flex; gap: 8px; justify-content: flex-end; align-items: center;
           margin-top: 14px; }
.buttons .err { color: var(--bad); margin-right: auto; font-size: 13px; }
.btn { border: 1px solid var(--line); background: var(--card); color: var(--text);
       border-radius: 6px; padding: 6px 14px; cursor: pointer; font: inherit; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.none { color: var(--muted); font-size: 13px; }
.copy { border: none; background: none; color: var(--muted); cursor: pointer; font: inherit;
        padding: 0 4px; font-size: 13px; }
.copy:hover { color: var(--accent); }
td.acts { width: 1%; white-space: nowrap; padding-right: 2px; }
tr.paused td:not(.acts) { opacity: .55; }
.run { font-size: 12px; color: var(--accent); }
.run .act.ico svg { width: 13px; height: 13px; }
h2 .act.ico { vertical-align: -3px; }
.act.ico, .copy { display: inline-flex; vertical-align: middle; }
.act.ico svg, .copy svg { width: 17px; height: 17px; fill: none; stroke: currentColor;
          stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.copy svg { width: 14px; height: 14px; }
.attrs td.cp { cursor: pointer; }
.attrs td.cp:hover { color: var(--accent); text-decoration: underline; }
#toast { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); z-index: 20;
         background: var(--text); color: var(--bg); padding: 6px 14px; border-radius: 99px;
         font-size: 13px; opacity: 0; transition: opacity .2s; pointer-events: none; }
#toast.show { opacity: .92; }
/* intestazione delle finestre: chiudi a sinistra, salva a destra, sempre visibile */
.mhead { position: sticky; top: -18px; z-index: 2; display: flex; align-items: center; gap: 10px;
  background: var(--card); margin: -18px -18px 10px; padding: 12px 18px;
  border-bottom: 1px solid var(--line); }
.mhead h3 { flex: 1; margin: 0; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mhead .err { color: var(--bad); font-size: 13px; }
.mhead .err:empty { display: none; }
.btn.close { padding: 4px 10px; font-size: 16px; line-height: 1; }
/* dashboard: riquadri che comandano */
.tile.cmd:active { transform: scale(.96); }
.tile.busy { opacity: .6; }
/* riquadro aperto: solo i bottoni, uno per comando (es. AC, DC) */
.tile.open { justify-content: flex-start; gap: 6px; background: var(--card); outline: 2px solid var(--rc); }
.tile .tname.small { font-size: 11px; display: block; white-space: nowrap; overflow: hidden;
  text-overflow: ellipsis; color: var(--muted); }
.tbtns { flex: 1; display: flex; flex-direction: column; gap: 4px; }
.tbtn { flex: 1; font: inherit; font-size: 13px; font-weight: 600; border-radius: 6px; cursor: pointer;
  border: 1px solid var(--line); background: var(--off-bg); color: var(--text); }
.tbtn.on { background: var(--on); border-color: var(--on); color: #fff; }
.tbtn.busy { opacity: .6; }
/* riquadri delle etichette in Dashboard: come le stanze, bordo tratteggiato */
.room.label { border-style: dashed; }
/* selettore di oggetti (widgets/picker.js): sopra gli altri riquadri */
.modal.pick { z-index: 60; }
.modal.wiz { z-index: 55; }
#pk-q { width: 100%; box-sizing: border-box; margin: 10px 0; font: inherit; padding: 8px 10px;
  border: 1px solid var(--line); border-radius: 6px; background: var(--card); color: var(--text); }
.pk-body { max-height: 60vh; overflow: auto; }
.pk-g { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .04em;
  margin: 10px 0 4px; }
.pk-r { display: flex; align-items: center; gap: 10px; padding: 6px 8px; border-radius: 6px; cursor: pointer; }
.pk-r:hover { background: var(--accent-bg); }
.pk-i { width: 20px; height: 20px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.8;
  stroke-linecap: round; stroke-linejoin: round; color: var(--muted); }
.pk-n { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.pk-n small { color: var(--muted); font-size: 11px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pk-v { color: var(--muted); font-size: 12px; max-width: 40%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pk-back { cursor: pointer; color: var(--accent); margin: 8px 0; font-weight: 600; }
.pk-dn { display: flex; align-items: center; gap: 8px; margin: 6px 0 10px; font-size: 13px; color: var(--muted); }
.pk-dn input { font: inherit; padding: 4px 8px; border: 1px solid var(--line); border-radius: 6px;
  background: var(--card); color: var(--text); }
.pk-dn input.bad { border-color: var(--bad); }
.pk-al { color: var(--accent); font-weight: 600; }
/* selettore: filtri House / Room / Search */
.pk-f { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 10px 0; }
.pk-f label { display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: var(--muted); }
.pk-f .pk-s { grid-column: 1 / -1; }
.pk-f select, .pk-f input { font: inherit; font-size: 14px; padding: 6px 8px; border: 1px solid var(--line);
  border-radius: 6px; background: var(--card); color: var(--text); }
.pk-f #pk-q { margin: 0; }
/* editor visuale delle routine (widgets/visual.js) */
.vis .vhead { display: flex; gap: 16px; align-items: flex-end; margin: 12px 0; flex-wrap: wrap; }
.vis .vhead label { display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: var(--muted); }
.vis .vhead input[type=text], .vis #vs-name { font: inherit; font-size: 15px; padding: 6px 8px; min-width: 260px;
  border: 1px solid var(--line); border-radius: 6px; background: var(--card); color: var(--text); }
.vis .vsw { flex-direction: row !important; align-items: center; gap: 6px !important; font-size: 14px !important; color: var(--text) !important; }
.vsec { background: var(--off-bg); border-radius: 10px; padding: 10px 12px; margin: 10px 0; }
.vsec h4 { margin: 0 0 8px; font-size: 13px; text-transform: uppercase; letter-spacing: .05em; }
.vsec h4 small { text-transform: none; letter-spacing: 0; font-weight: 400; color: var(--muted); margin-left: 6px; }
.vsec h4 .vkw { text-transform: none; letter-spacing: 0; font-weight: 400; font-size: 12px; color: var(--muted); background: none; padding: 0; margin-left: 4px; }
.vmanual { font-style: italic; margin: 2px 0 6px; }
.vr { display: flex; align-items: center; gap: 8px; padding: 6px 4px; border-bottom: 1px solid var(--line); }
.vr .vt { flex: 1; cursor: pointer; white-space: pre-wrap; word-break: break-word; font-size: 13px; }
.vr .vt:hover { color: var(--accent); }
.vgrip { cursor: grab; color: var(--muted); user-select: none; }
.vdel { font-size: 18px; color: var(--accent); }
.vadd { margin-top: 6px; font-weight: 600; padding: 4px 0; }
.vhomes { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.vhomes .chip { cursor: pointer; }
.vopt { display: flex; align-items: center; gap: 6px; margin-top: 8px; font-size: 13px; }
.vbody { display: flex; flex-direction: column; gap: 10px; }
.vbody label { display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: var(--muted); }
.vbody input { font: inherit; font-size: 14px; padding: 7px 9px; border: 1px solid var(--line); border-radius: 6px;
  background: var(--card); color: var(--text); }
.vchoices { display: flex; gap: 6px; flex-wrap: wrap; }
.vchoices .chip.sel { background: var(--accent); color: #fff; border-color: var(--accent); }
.vtext { display: flex; gap: 6px; } .vtext input { flex: 1; }
/* porta pubblica: menu dell'account in alto a destra */
.account { position: relative; margin-left: 10px; }
.account summary { cursor: pointer; list-style: none; padding: 4px 10px; border-radius: 14px; background: var(--off-bg); font-size: 13px; }
.account summary::-webkit-details-marker { display: none; }
.account .menu { position: absolute; right: 0; top: 130%; z-index: 40; background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 10px; display: flex; flex-direction: column; gap: 6px; min-width: 220px; box-shadow: 0 6px 20px rgba(0,0,0,.15); }
