:root {
  --bg: #050B17;
  --bg2: #0A1528;
  --card: #0D1B33;
  --card2: #112442;
  --line: #1C3358;
  --blue: #0098FB;
  --cyan: #05E0FC;
  --text: #E8F1FF;
  --muted: #8BA3C7;
  --ok: #3DDC97;
  --warn: #F5B83D;
  --bad: #FF5C7A;
  --pv: #F5B83D;
  --load: #05E0FC;
  --batt: #3DDC97;
  --grid: #FF6B8A;
  --soc: #A99BFF;
  --radius: 16px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.5 Montserrat, system-ui, sans-serif; -webkit-font-smoothing: antialiased; }
body { min-height: 100vh; background: radial-gradient(1200px 600px at 80% -10%, rgba(0,152,251,.14), transparent 60%), radial-gradient(900px 500px at -10% 110%, rgba(5,224,252,.08), transparent 60%), var(--bg); background-attachment: fixed; }
h1, h2, h3 { font-family: 'Exo 2', sans-serif; font-style: italic; font-weight: 700; margin: 0; letter-spacing: .01em; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
a { color: var(--cyan); }
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: .82rem; }
.err { color: var(--bad); min-height: 1.2em; }
svg { fill: currentColor; }

/* ---------- login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.login-card { width: min(380px, 100%); background: linear-gradient(180deg, var(--card2), var(--card)); border: 1px solid var(--line); border-radius: 22px; padding: 32px 28px; text-align: center; box-shadow: 0 30px 80px rgba(0,0,0,.5), 0 0 0 1px rgba(5,224,252,.05) inset; }
.login-card img { border-radius: 18px; box-shadow: 0 0 40px rgba(0,152,251,.35); }
.login-card h1 { font-size: 2rem; margin-top: 12px; background: linear-gradient(90deg, var(--blue), var(--cyan)); -webkit-background-clip: text; background-clip: text; color: transparent; }
.login-card label { display: block; text-align: left; margin: 14px 0 0; font-size: .85rem; color: var(--muted); }
.login-card input { margin-top: 6px; }
.login-card .btn { width: 100%; margin-top: 20px; }

/* ---------- inputs & buttons ---------- */
input, select, textarea { width: 100%; background: var(--bg2); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; outline: none; }
input:focus, select:focus, textarea:focus { border-color: var(--blue); box-shadow: 0 0 0 3px rgba(0,152,251,.2); }
input[type=checkbox] { width: auto; }
input[type=range] { padding: 0; accent-color: var(--cyan); background: none; border: 0; }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; border: 1px solid var(--line); background: var(--card2); border-radius: 999px; padding: 9px 18px; font-weight: 600; cursor: pointer; transition: .15s; white-space: nowrap; }
.btn:hover { border-color: var(--blue); }
.btn.primary { background: linear-gradient(90deg, var(--blue), #00b8f5); border-color: transparent; color: #fff; box-shadow: 0 6px 20px rgba(0,152,251,.35); }
.btn.primary:hover { filter: brightness(1.1); }
.btn.danger { border-color: rgba(255,92,122,.5); color: #ffb3c1; }
.btn.small { padding: 5px 12px; font-size: .82rem; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.icon-btn { position: relative; width: 40px; height: 40px; border-radius: 12px; border: 1px solid var(--line); background: var(--card); display: grid; place-items: center; cursor: pointer; }
.icon-btn svg { width: 20px; height: 20px; }
.icon-btn .count { position: absolute; top: -5px; right: -5px; background: var(--bad); color: #fff; font-size: .7rem; font-weight: 700; border-radius: 999px; padding: 0 6px; min-width: 18px; line-height: 18px; text-align: center; }

/* ---------- shell ---------- */
.shell { display: grid; grid-template-columns: 240px 1fr; grid-template-rows: auto 1fr; min-height: 100vh; }
.side { grid-row: 1 / 3; background: rgba(10,21,40,.7); border-right: 1px solid var(--line); padding: 20px 14px; display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; backdrop-filter: blur(8px); }
.brand { display: flex; align-items: center; gap: 10px; font-family: 'Exo 2'; font-style: italic; font-weight: 800; font-size: 1.35rem; }
.brand img { border-radius: 10px; }
.brand span { background: linear-gradient(90deg, var(--blue), var(--cyan)); -webkit-background-clip: text; background-clip: text; color: transparent; }
#nav { margin-top: 26px; display: flex; flex-direction: column; gap: 4px; }
.nav-item { display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-radius: 12px; color: var(--muted); text-decoration: none; font-weight: 600; border: 1px solid transparent; cursor: pointer; background: none; text-align: left; }
.nav-item svg { width: 20px; height: 20px; flex: none; }
.nav-item:hover { color: var(--text); background: var(--card); }
.nav-item.active { color: #fff; background: linear-gradient(90deg, rgba(0,152,251,.25), rgba(5,224,252,.06)); border-color: rgba(0,152,251,.35); }
.nav-item.active svg { color: var(--cyan); }
.side-foot { margin-top: auto; padding: 0 12px; }
.top { display: flex; align-items: center; gap: 16px; padding: 18px 28px; position: sticky; top: 0; z-index: 5; background: rgba(5,11,23,.75); backdrop-filter: blur(12px); border-bottom: 1px solid rgba(28,51,88,.6); }
.top h1 { font-size: 1.5rem; }
.top-right { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.badge { font-size: .72rem; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; padding: 4px 10px; border-radius: 999px; border: 1px solid var(--line); color: var(--muted); }
.badge.demo { color: var(--warn); border-color: rgba(245,184,61,.45); background: rgba(245,184,61,.08); }
.badge.live { color: var(--ok); border-color: rgba(61,220,151,.45); background: rgba(61,220,151,.08); }
.live-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--bad); box-shadow: 0 0 0 3px rgba(255,92,122,.15); }
.live-dot.on { background: var(--ok); box-shadow: 0 0 0 3px rgba(61,220,151,.18), 0 0 12px var(--ok); }
.live-dot.poll { background: var(--warn); box-shadow: 0 0 0 3px rgba(245,184,61,.18); }
main { padding: 24px 28px 40px; outline: none; max-width: 1400px; width: 100%; }
.tabbar { display: none; }
.mobile-only { display: none; }

/* ---------- layout pieces ---------- */
.grid { display: grid; gap: 18px; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.cards { grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); }
.span2 { grid-column: span 2; }
.main-side { grid-template-columns: minmax(0, 2fr) minmax(0, 1fr); }
@media (max-width: 1100px) { .main-side { grid-template-columns: 1fr; } }
.card { background: linear-gradient(180deg, rgba(17,36,66,.9), rgba(13,27,51,.9)); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; min-width: 0; }
.card-h { display: flex; align-items: center; gap: 10px; margin-bottom: 14px; flex-wrap: wrap; }
.card-h .right { margin-left: auto; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.section { margin-top: 26px; }
.section > .card-h { margin-bottom: 12px; }
.row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.spread { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.empty { padding: 30px; text-align: center; color: var(--muted); border: 1px dashed var(--line); border-radius: var(--radius); }
.pill { display: inline-flex; align-items: center; gap: 6px; font-size: .75rem; font-weight: 600; padding: 3px 10px; border-radius: 999px; background: var(--bg2); border: 1px solid var(--line); color: var(--muted); }
.pill.ok { color: var(--ok); border-color: rgba(61,220,151,.35); }
.pill.bad { color: var(--bad); border-color: rgba(255,92,122,.4); }
.pill.warn { color: var(--warn); border-color: rgba(245,184,61,.4); }
.stat { display: flex; flex-direction: column; }
.stat b { font-family: 'Exo 2'; font-style: italic; font-size: 1.6rem; font-weight: 700; line-height: 1.1; }
.stat span { color: var(--muted); font-size: .78rem; }
.tabs { display: inline-flex; background: var(--bg2); border: 1px solid var(--line); border-radius: 999px; padding: 3px; }
.tabs button { border: 0; background: none; padding: 6px 14px; border-radius: 999px; cursor: pointer; color: var(--muted); font-weight: 600; font-size: .85rem; }
.tabs button.on { background: var(--blue); color: #fff; }

/* ---------- toggle ---------- */
.toggle { position: relative; width: 50px; height: 28px; flex: none; border-radius: 999px; background: #233a5e; border: 0; cursor: pointer; transition: .2s; }
.toggle::after { content: ''; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #c5d4ea; transition: .2s; }
.toggle.on { background: linear-gradient(90deg, var(--blue), var(--cyan)); box-shadow: 0 0 14px rgba(5,224,252,.4); }
.toggle.on::after { left: 25px; background: #fff; }
.toggle:disabled { opacity: .4; cursor: not-allowed; }

/* ---------- devices ---------- */
.dev { display: flex; flex-direction: column; gap: 12px; }
.dev.offline { opacity: .55; }
.dev-ic { width: 42px; height: 42px; border-radius: 12px; display: grid; place-items: center; background: var(--bg2); border: 1px solid var(--line); color: var(--muted); flex: none; }
.dev-ic svg { width: 22px; height: 22px; }
.dev-ic.on { color: var(--cyan); border-color: rgba(5,224,252,.4); box-shadow: 0 0 18px rgba(5,224,252,.18) inset; }
.ch { display: flex; align-items: center; justify-content: space-between; padding: 8px 0; border-top: 1px solid rgba(28,51,88,.6); }
.ch:first-child { border-top: 0; }
.metrics { display: flex; gap: 18px; flex-wrap: wrap; }
.metrics .stat b { font-size: 1.15rem; }

/* ---------- scenes ---------- */
.scenes { display: flex; gap: 12px; flex-wrap: wrap; }
.scene { display: flex; align-items: center; gap: 10px; padding: 12px 18px; border-radius: 14px; border: 1px solid var(--line); background: var(--card); cursor: pointer; font-weight: 600; }
.scene:hover { border-color: var(--cyan); box-shadow: 0 0 18px rgba(5,224,252,.15); }
.scene svg { width: 20px; height: 20px; color: var(--cyan); }

/* ---------- energy flow ---------- */
.flow svg { width: 100%; height: auto; max-height: 360px; display: block; }
.flow .node circle { fill: var(--bg2); stroke-width: 2; }
.flow .node text { fill: var(--text); font-family: Montserrat; text-anchor: middle; }
.flow .node .v { font-family: 'Exo 2'; font-style: italic; font-weight: 700; font-size: 17px; }
.flow .node .l { fill: var(--muted); font-size: 11px; }
.flow path.wire { fill: none; stroke: #1f3a63; stroke-width: 3; }
.flow path.dash { fill: none; stroke-width: 3; stroke-dasharray: 6 12; stroke-linecap: round; animation: flow 1s linear infinite; }
.flow path.dash.rev { animation-direction: reverse; }
.flow path.dash.idle { display: none; }
@keyframes flow { to { stroke-dashoffset: -18; } }
@media (prefers-reduced-motion: reduce) { .flow path.dash { animation: none; } }

/* ---------- charts ---------- */
.chart { position: relative; }
.chart svg { width: 100%; height: 280px; display: block; }
.chart .axis text { fill: var(--muted); font-size: 11px; }
.chart .axis line { stroke: #1a2f52; }
.chart .tip { position: absolute; pointer-events: none; background: rgba(5,11,23,.95); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; font-size: .8rem; white-space: nowrap; transform: translate(-50%, -110%); }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: .8rem; color: var(--muted); margin-top: 8px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }

/* ---------- cameras ---------- */
.cams { display: grid; gap: 14px; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
.cam { position: relative; border-radius: 14px; overflow: hidden; border: 1px solid var(--line); background: #000; cursor: pointer; aspect-ratio: 16 / 9; }
.cam img { width: 100%; height: 100%; object-fit: cover; display: block; }
.cam .cap { position: absolute; left: 0; right: 0; bottom: 0; padding: 22px 12px 8px; background: linear-gradient(transparent, rgba(0,0,0,.8)); display: flex; justify-content: space-between; align-items: end; font-weight: 600; font-size: .9rem; }
.viewer img { width: 100%; border-radius: 12px; background: #000; display: block; aspect-ratio: 16/9; object-fit: contain; }
.events { display: flex; flex-direction: column; gap: 10px; }
.event { display: flex; gap: 12px; align-items: center; padding: 8px; border-radius: 12px; background: var(--bg2); border: 1px solid var(--line); }
.event img { width: 112px; aspect-ratio: 16/9; object-fit: cover; border-radius: 8px; flex: none; cursor: pointer; background: #000; }
.seg { display: flex; justify-content: space-between; align-items: center; padding: 8px 10px; border-radius: 10px; background: var(--bg2); border: 1px solid var(--line); cursor: pointer; }
.seg:hover, .seg.on { border-color: var(--cyan); }
.segs { display: flex; flex-direction: column; gap: 6px; max-height: 360px; overflow: auto; }

/* ---------- lists ---------- */
.list { display: flex; flex-direction: column; gap: 10px; }
.item { display: flex; gap: 14px; align-items: flex-start; padding: 14px; border-radius: 14px; background: var(--card); border: 1px solid var(--line); }
.item.unread { border-color: rgba(0,152,251,.5); }
.item .grow { flex: 1; min-width: 0; }
.item img { width: 140px; border-radius: 10px; aspect-ratio: 16/9; object-fit: cover; flex: none; background: #000; }
.lvl { width: 10px; height: 10px; border-radius: 50%; margin-top: 7px; flex: none; background: var(--blue); }
.lvl.warning { background: var(--warn); }
.lvl.alert { background: var(--bad); box-shadow: 0 0 10px var(--bad); }
.rule-sum { color: var(--muted); font-size: .85rem; margin-top: 4px; }
.rule-sum b { color: var(--text); font-weight: 600; }

/* ---------- forms in modals ---------- */
.form { display: flex; flex-direction: column; gap: 14px; }
.form label { font-size: .82rem; color: var(--muted); display: flex; flex-direction: column; gap: 6px; }
.form .inline { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 10px; }
.box { border: 1px solid var(--line); border-radius: 12px; padding: 12px; display: flex; flex-direction: column; gap: 10px; background: rgba(5,11,23,.4); }
.days { display: flex; gap: 4px; flex-wrap: wrap; }
.days button { width: 38px; height: 34px; border-radius: 8px; border: 1px solid var(--line); background: var(--bg2); cursor: pointer; font-size: .78rem; font-weight: 600; color: var(--muted); }
.days button.on { background: var(--blue); color: #fff; border-color: var(--blue); }
.diff { width: 100%; border-collapse: collapse; }
.diff td, .diff th { padding: 8px; border-bottom: 1px solid var(--line); text-align: left; }
.diff .to { color: var(--cyan); font-weight: 700; }

/* ---------- modal & toast ---------- */
.modal { position: fixed; inset: 0; background: rgba(2,6,14,.75); backdrop-filter: blur(6px); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal-card { position: relative; width: min(720px, 100%); max-height: 92vh; overflow: auto; background: linear-gradient(180deg, var(--card2), var(--card)); border: 1px solid var(--line); border-radius: 20px; padding: 22px; box-shadow: 0 30px 90px rgba(0,0,0,.6); }
.modal-card.wide { width: min(1000px, 100%); }
.modal-x { position: absolute; top: 12px; right: 14px; width: 36px; height: 36px; border-radius: 50%; border: 1px solid var(--line); background: var(--bg2); font-size: 22px; line-height: 1; cursor: pointer; z-index: 2; }
.modal h2 { margin: 0 40px 16px 0; }
.toasts { position: fixed; right: 18px; bottom: 18px; display: flex; flex-direction: column; gap: 10px; z-index: 60; max-width: min(380px, calc(100% - 32px)); }
.toast { background: var(--card2); border: 1px solid var(--line); border-left: 4px solid var(--blue); border-radius: 12px; padding: 12px 14px; box-shadow: 0 14px 40px rgba(0,0,0,.5); animation: tin .25s ease-out; }
.toast.warning { border-left-color: var(--warn); }
.toast.alert, .toast.error { border-left-color: var(--bad); }
.toast b { display: block; font-size: .9rem; }
@keyframes tin { from { transform: translateY(10px); opacity: 0; } }

/* ---------- mobile ---------- */
@media (max-width: 900px) {
  .g3 { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 760px) {
  .shell { grid-template-columns: 1fr; }
  .side { display: none; }
  .mobile-only { display: flex; }
  .top { padding: 12px 16px; padding-top: max(12px, env(safe-area-inset-top)); gap: 10px; }
  .top h1 { display: none; }
  .badge { display: none; }
  main { padding: 16px 16px calc(90px + var(--safe-b)); }
  .g2, .g3 { grid-template-columns: 1fr; }
  .span2 { grid-column: auto; }
  .tabbar { display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; background: rgba(8,17,33,.95); backdrop-filter: blur(12px); border-top: 1px solid var(--line); padding: 6px 4px calc(6px + var(--safe-b)); justify-content: space-around; }
  .tabbar .nav-item { flex-direction: column; gap: 2px; padding: 6px 4px; font-size: .66rem; flex: 1; align-items: center; border-radius: 10px; }
  .tabbar .nav-item svg { width: 22px; height: 22px; }
  .item img, .event img { width: 96px; }
  .toasts { left: 16px; right: 16px; bottom: calc(84px + var(--safe-b)); max-width: none; }
  .chart svg { height: 230px; }
  .card-h .right { margin-left: 0; width: 100%; flex-wrap: nowrap; }
  .card-h .right input[type=search] { max-width: none !important; flex: 1; min-width: 0; }
  .card-h .right .pill { white-space: nowrap; }
  .card-h h2 { font-size: 1.05rem; }
}
.cam .cap span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; margin-right: 8px; }
