:root {
    --bg: #f4f1eb;
    --bg-2: #fff;
    --card: #ffffff;
    --line: #e4ddd3;
    --text: #1c1917;
    --muted: #6f675e;
    --red: #d61f26;
    --amber: #c47a12;
    --green: #1f8a5b;
    --shadow: 0 10px 30px rgba(28, 25, 23, .06);
    --font: "Inter", system-ui, sans-serif;
    --display: "Inter", system-ui, sans-serif;
    --mono: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
    font-family: var(--font);
    background: var(--bg);
    color: var(--text);
}
a { color: inherit; }
button, input, textarea, select { font: inherit; }
.mono { font-family: var(--mono); }

.shell { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.rail {
    position: sticky; top: 0; height: 100vh;
    padding: 24px 18px;
    border-right: 1px solid var(--line);
    background: #fff;
    display: flex; flex-direction: column; gap: 24px;
}
.brand { display: flex; gap: 10px; align-items: center; text-decoration: none; color: var(--red); }
.brand-mark {
    width: 40px; height: 40px; display: grid; place-items: center;
    background: #fde8e8; border-radius: 10px;
}
.brand strong { display: block; font-family: var(--display); font-size: 18px; color: var(--text); }
.brand small { color: var(--muted); letter-spacing: .16em; font-size: 10px; }
.nav { display: flex; flex-direction: column; gap: 4px; }
.nav a {
    text-decoration: none; padding: 10px 12px; color: var(--muted); border-radius: 8px;
}
.nav a.is-on, .nav a:hover { color: var(--text); background: #f4f1eb; }
.rail-foot { margin-top: auto; display: flex; justify-content: space-between; align-items: end; gap: 12px; }
.who small { display: block; color: var(--muted); font-size: 12px; }
.linkish { background: none; border: 0; color: #3f3a36; cursor: pointer; padding: 0; text-decoration: underline; }

.stage { padding: 32px 36px 64px; max-width: 1200px; }
.head { display: flex; justify-content: space-between; align-items: end; gap: 16px; margin-bottom: 24px; }
.head h1 { margin: 0; font-family: var(--display); font-size: clamp(28px, 4vw, 44px); line-height: .95; }
.head h1.mono { font-family: var(--mono); font-size: 28px; }
.kicker { margin: 0 0 8px; color: var(--red); font-size: 12px; }
.kicker a { color: var(--red); }
.head-meta { display: flex; gap: 8px; flex-wrap: wrap; }

.chip, .pill {
    display: inline-flex; align-items: center; gap: 8px;
    border: 1px solid var(--line); padding: 6px 10px; font-size: 12px;
    background: #fff; border-radius: 999px;
}
.chip[data-state="connected"] { color: var(--green); border-color: #bfe8d2; background: #edfaf3; }
.chip[data-state="qr"] { color: var(--amber); }
.chip[data-state="down"], .chip[data-state="disconnected"] { color: var(--red); }

.stats { display: grid; grid-template-columns: repeat(6, 1fr); gap: 12px; margin-bottom: 20px; }
.stat { background: var(--card); border: 1px solid var(--line); padding: 16px; border-radius: 12px; box-shadow: var(--shadow); }
.stat span { display: block; color: var(--muted); font-size: 12px; }
.stat strong { font-family: var(--display); font-size: 30px; }
.stat.alarm strong { color: var(--red); }
.stat.fault strong { color: var(--amber); }

.alarm-banner { background: var(--red); color: #fff; padding: 12px 16px; margin-bottom: 20px; border-radius: 10px; font-weight: 600; }
.is-hidden { display: none !important; }

.section-title { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; margin-bottom: 12px; }
.section-title h2, .panel h2 { margin: 0 0 8px; font-size: 18px; font-family: var(--display); }
.muted { color: var(--muted); }
.section-title a:not(.btn) { color: var(--red); }

.split { display: grid; grid-template-columns: 1.4fr .8fr; gap: 24px; }
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; margin-bottom: 18px; }
.panel { background: var(--card); border: 1px solid var(--line); padding: 20px; margin-bottom: 18px; border-radius: 14px; box-shadow: var(--shadow); }
.stack { display: flex; flex-direction: column; gap: 12px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

label span { display: block; margin-bottom: 6px; color: var(--muted); font-size: 12px; }
input, textarea, select {
    width: 100%; background: #faf8f4; border: 1px solid var(--line);
    padding: 10px 12px; outline: none; border-radius: 8px;
}
select {
    appearance: none;
    background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'><path fill='%231c1917' d='M2 2h8L6 7z'/></svg>");
    background-repeat: no-repeat;
    background-position: right 12px center;
    padding-right: 36px;
}
input:focus, textarea:focus, select:focus { border-color: #1c1917; background: #fff; }

.check-row { display: flex; flex-wrap: wrap; gap: 10px; }
.toggle, .radio {
    display: inline-flex; align-items: center; gap: 8px;
    margin: 0; padding: 8px 12px;
    background: #fff; border: 1px solid var(--line); border-radius: 999px;
    cursor: pointer; user-select: none; font-size: 13px; color: var(--text);
}
.toggle input, .radio input, .check input {
    appearance: none; -webkit-appearance: none;
    margin: 0; padding: 0; border: 0; outline: none;
    flex: 0 0 auto; cursor: pointer;
}
.toggle input {
    width: 40px; height: 22px; border-radius: 999px;
    background: #d9d2c8; position: relative; transition: background .15s;
}
.toggle input::after {
    content: ""; position: absolute; top: 3px; left: 3px;
    width: 16px; height: 16px; border-radius: 50%; background: #fff;
    box-shadow: 0 1px 3px rgba(28,25,23,.25); transition: transform .15s;
}
.toggle input:checked { background: var(--green); }
.toggle input:checked::after { transform: translateX(18px); }
.toggle:has(input:checked), .radio:has(input:checked) {
    border-color: #bfe8d2; background: #edfaf3;
}
.toggle span, .radio span {
    display: inline; margin: 0; color: inherit; font-size: 13px;
}
.radio input {
    width: 18px; height: 18px; border-radius: 50%;
    border: 2px solid #cfc7bc; background: #fff;
}
.radio input:checked {
    border-color: var(--red);
    background: radial-gradient(circle at center, var(--red) 0 5px, #fff 6px 100%);
}
.toggle input:focus-visible, .radio input:focus-visible {
    outline: 2px solid var(--text); outline-offset: 2px;
}

.file-grid { align-items: start; }
.file-drop { position: relative; display: flex; flex-direction: column; }
.file-drop input[type="file"] {
    position: absolute; inset: 26px 0 0 0; opacity: 0; width: 100%; height: calc(100% - 26px);
    cursor: pointer; padding: 0; border: 0; background: transparent;
}
.file-drop-body {
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: 4px; min-height: 92px; padding: 16px 12px; text-align: center;
    border: 1.5px dashed var(--line); border-radius: 12px; background: #faf8f4;
    color: var(--text); font-size: 14px; margin: 0;
}
.file-drop-body strong { font-size: 14px; }
.file-drop-body small { color: var(--muted); font-size: 12px; }
.file-drop-body em { font-style: normal; font-size: 12px; color: var(--muted); }
.file-drop:hover .file-drop-body, .file-drop.is-on .file-drop-body {
    border-color: var(--text); background: #fff;
}
.file-drop.has-file .file-drop-body {
    border-style: solid; border-color: #8fd0ad; background: #edfaf3;
}
.file-drop.has-file em { color: var(--green); font-weight: 600; }
.btn {
    display: inline-flex; align-items: center; justify-content: center;
    background: #fff; border: 1px solid var(--line); padding: 10px 14px;
    cursor: pointer; text-decoration: none; border-radius: 8px;
    color: var(--text);
}
.btn:hover, .btn.is-on { border-color: var(--text); color: var(--text); }
.btn-fire,
a.btn-fire {
    background: var(--red);
    border-color: var(--red);
    color: #fff !important;
    font-weight: 700;
}
.btn-fire:hover,
a.btn-fire:hover { color: #fff !important; border-color: #b51a20; background: #b51a20; }
.btn-ghost { opacity: .85; color: var(--text); background: #fff; }
.btn-row, .tab-row { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }
.btn-sm { padding: 8px 10px; font-size: 13px; white-space: nowrap; }

.zone-defs { list-style: none; margin: 0 0 12px; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.zone-defs li {
    display: grid; grid-template-columns: 88px minmax(0, 1fr) auto;
    gap: 8px; align-items: center;
}
.zone-defs form { margin: 0; }
.zone-defs strong { font-size: 13px; }

.device-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 12px; }
.device-card {
    position: relative; display: block; text-decoration: none;
    background: var(--card); border: 1px solid var(--line); padding: 16px 16px 16px 22px;
    border-radius: 14px; box-shadow: var(--shadow);
}
.device-card h3 { margin: 10px 0 6px; font-size: 16px; }
.device-card p { margin: 0; color: var(--muted); font-size: 13px; }
.device-card-top { display: flex; justify-content: space-between; gap: 8px; align-items: center; }
.device-card code { font-family: var(--mono); font-size: 12px; color: var(--red); }
.led { position: absolute; left: 0; top: 0; bottom: 0; width: 6px; background: var(--green); border-radius: 14px 0 0 14px; }
.health-alarm .led, .pill.health-alarm { background: var(--red); color: #fff; }
.health-fault .led, .pill.health-fault { background: var(--amber); color: #fff; }
.health-offline .led { background: #c4bdb3; }
.health-alarm { box-shadow: 0 0 0 1px rgba(214, 31, 38, .25); }

.event-list { list-style: none; margin: 0; padding: 0; }
.event-list li { display: flex; justify-content: space-between; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line); font-size: 13px; }
.event-list strong { display: block; }
.event-list span, .event-list time { color: var(--muted); }
.event-list .is-alarm strong { color: var(--red); }
.event-list .is-fault strong { color: var(--amber); }

.flash { padding: 12px 14px; margin-bottom: 16px; border: 1px solid var(--line); border-radius: 10px; background: #fff; }
.flash-ok { border-color: #bfe8d2; color: var(--green); }
.flash-err { border-color: #f5c2c5; color: var(--red); }
.wizard { background: #fff6e8; border: 1px solid #f0d7aa; padding: 14px 16px; margin-bottom: 18px; border-radius: 10px; }
.empty { color: var(--muted); padding: 24px; border: 1px dashed var(--line); border-radius: 12px; }

.people { list-style: none; margin: 0; padding: 0; }
.people li {
    display: grid; grid-template-columns: minmax(0, 1fr) auto auto;
    gap: 12px; align-items: center; padding: 12px 0; border-bottom: 1px solid var(--line);
}
.people span { display: block; color: var(--muted); }
.people .toggle span { display: inline; color: inherit; }
.people form { margin: 0; }
.person-flags { display: flex; flex-wrap: wrap; gap: 8px; }

.table { width: 100%; border-collapse: collapse; font-size: 13px; }
.table th, .table td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--line); }
.table .is-alarm { color: var(--red); }
.table .is-fault { color: var(--amber); }
.table a { color: var(--red); }

.login-body { min-height: 100vh; display: grid; place-items: center; padding: 24px; background: var(--bg); }
.login-wrap { width: min(420px, 100%); }
.login-card { padding: 36px; background: #fff; border: 1px solid var(--line); border-radius: 16px; box-shadow: var(--shadow); }
.login-kicker { color: var(--red); letter-spacing: .18em; font-size: 11px; margin-bottom: 10px; }
.login-card h1 { font-family: var(--display); font-size: 40px; margin: 0 0 18px; }

.wa-box { min-height: 240px; display: grid; place-items: center; border: 1px solid var(--line); padding: 16px; background: #fff; margin: 12px 0; border-radius: 12px; }
.wa-qr { width: min(280px, 100%); }
.wa-ok { text-align: center; color: var(--green); }
.wa-ok span { display: block; }
.wa-log td strong + span { display: block; color: var(--muted); font-size: 12px; }
.wa-log details summary { cursor: pointer; max-width: 420px; }
.wa-msg {
    margin: 8px 0 0; padding: 10px 12px; background: #faf8f4;
    border: 1px solid var(--line); border-radius: 8px;
    white-space: pre-wrap; font-size: 12px; max-height: 220px; overflow: auto;
}
.pager { display: flex; align-items: center; gap: 10px; margin-top: 14px; }
.pager span { color: var(--muted); font-size: 13px; }
.pill.status-sent { color: var(--green); border-color: #bfe8d2; background: #edfaf3; }
.pill.status-failed { color: var(--red); border-color: #f5c2c5; background: #fde8e8; }
.pill.status-queued { color: var(--amber); }
.map-layout { display: grid; grid-template-columns: 280px 1fr; gap: 16px; }
.zone-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.zone-list li { display: flex; flex-direction: column; gap: 6px; padding: 8px; background: #faf8f4; border: 1px solid var(--line); border-radius: 8px; }
.zone-pick { display: flex; gap: 8px; align-items: center; width: 100%; background: #fff; color: var(--text); border: 1px solid var(--line); padding: 8px 10px; border-radius: 8px; cursor: pointer; }
.zone-pick i { width: 12px; height: 12px; border-radius: 3px; }
.zone-list li.is-on { border-color: var(--text); }
.map-hint { margin: 0 0 12px; font-size: 13px; }
.zone-list .btn { width: 100%; }
.map-canvas-hold { overflow: auto; background: #eee; border: 1px solid var(--line); border-radius: 10px; }
#mapCanvas { display: block; max-width: 100%; cursor: crosshair; background: #fff; }
.plan-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 16px; }
.plan-card { margin: 0; background: #faf8f4; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
.plan-card figcaption { padding: 10px 12px; font-weight: 600; }
.plan-card canvas { display: block; width: 100%; background: #ddd; }
.danger-zone { border-color: #f5c2c5; }

@media (max-width: 980px) {
    .shell { grid-template-columns: 1fr; }
    .rail { position: relative; height: auto; flex-direction: row; flex-wrap: wrap; }
    .nav { flex-direction: row; }
    .rail-foot { margin: 0; width: 100%; }
    .stats, .split, .two-col, .map-layout { grid-template-columns: 1fr; }
    .zone-defs li, .people li { grid-template-columns: 1fr; }
    .stage { padding: 20px; }
}
