:root {
  --bg: #0d1117;
  --bg-elev: #11161d;
  --panel: #161b22;
  --panel-2: #1c2330;
  --line: #2a3340;
  --line-soft: #243041;
  --text: #e8eef6;
  --muted: #8b97a8;
  --accent: #2ea043;
  --accent-hover: #3fb950;
  --accent-dim: #238636;
  --accent-soft: rgba(46, 160, 67, 0.14);
  --blue: #316cf4;
  --blue-soft: rgba(49, 108, 244, 0.16);
  --purple: #8b5cf6;
  --danger: #d45b5b;
  --input: #0e1218;
  --radius: 12px;
  --radius-sm: 8px;
  --mono: "Cascadia Code", "Consolas", "SFMono-Regular", monospace;
  --sans: "Segoe UI", "IBM Plex Sans", system-ui, sans-serif;
  --topbar-h: 56px;
  --nav-h: 52px;
  --footer-h: 64px;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 14.5px/1.45 var(--sans);
  -webkit-font-smoothing: antialiased;
}
#app {
  max-width: 1280px;
  margin: 0 auto;
  padding: 0 1.25rem 2rem;
}
#dash-view:not([hidden]) {
  padding-bottom: 1rem;
}

/* —— Login —— */
.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1.5rem;
}
.login-panel { max-width: 420px; margin: 10vh auto 0; }
.login-panel h1 { margin: 0 0 .25rem; font-size: 1.5rem; letter-spacing: .02em; }
.login-panel form button[type="submit"] {
  display: block;
  width: 100%;
  margin-top: 1.1rem;
  text-align: center;
}
.login-panel input:-webkit-autofill,
.login-panel input:-webkit-autofill:hover,
.login-panel input:-webkit-autofill:focus {
  -webkit-text-fill-color: var(--text);
  caret-color: var(--text);
  box-shadow: 0 0 0 1000px var(--input) inset;
  transition: background-color 99999s ease-out;
}

.muted { color: var(--muted); }
.error { color: var(--danger); margin-top: 1rem; }
.status {
  margin-top: 1rem;
  padding: .75rem 1rem;
  border-radius: var(--radius-sm);
  background: #152018;
  border: 1px solid var(--accent-dim);
  color: #b7e0c7;
}
.status.err {
  background: #241616;
  border-color: #6a3030;
  color: #f0b4b4;
}

/* —— Forms —— */
form label {
  display: block;
  margin: .85rem 0;
  font-size: .85rem;
  color: var(--muted);
}
input, textarea, button, select {
  font: inherit;
  color: var(--text);
}
input, textarea, select {
  display: block;
  width: 100%;
  margin-top: .35rem;
  padding: .55rem .7rem;
  background: var(--input);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
}
input[type="number"] {
  -moz-appearance: textfield;
  appearance: textfield;
}
input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}
select {
  appearance: none;
  cursor: pointer;
  background-image:
    linear-gradient(45deg, transparent 50%, var(--muted) 50%),
    linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position:
    calc(100% - 16px) calc(50% - 2px),
    calc(100% - 10px) calc(50% - 2px);
  background-size: 6px 6px, 6px 6px;
  background-repeat: no-repeat;
  padding-right: 1.8rem;
}
.hint {
  color: var(--muted);
  font-size: .88rem;
  margin: 0 0 1rem;
  max-width: 52rem;
}
.hint a, .hint button.linkish {
  color: #8fc9a8;
  background: none;
  border: 0;
  padding: 0;
  font: inherit;
  cursor: pointer;
  text-decoration: underline;
}
textarea {
  font-family: var(--mono);
  font-size: 12.5px;
  line-height: 1.45;
  min-height: 220px;
  resize: vertical;
  background: #0a0e14;
}
button {
  cursor: pointer;
  border: 0;
  border-radius: var(--radius-sm);
  padding: .55rem 1rem;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
}
button:hover { background: var(--accent-hover); }
button:disabled { opacity: .55; cursor: wait; }
button.ghost {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--line);
  font-weight: 500;
}
button.ghost:hover { border-color: var(--muted); background: rgba(255,255,255,.03); }
button.btn-blue {
  background: var(--blue);
  color: #fff;
}
button.btn-blue:hover { filter: brightness(1.08); }
button.btn-outline-blue {
  background: transparent;
  color: #8eb0ff;
  border: 1px solid rgba(49, 108, 244, 0.55);
  font-weight: 500;
}
button.btn-outline-blue:hover {
  background: var(--blue-soft);
  border-color: var(--blue);
}

/* —— Topbar —— */
.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  min-height: var(--topbar-h);
  margin: 0 -1.25rem;
  padding: .65rem 1.25rem;
  background: rgba(13, 17, 23, 0.92);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.topbar-left {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: .55rem .75rem;
  min-width: 0;
}
.device-id {
  font-family: var(--mono);
  font-size: .92rem;
  letter-spacing: .02em;
}
.online-pill {
  display: inline-flex;
  align-items: center;
  gap: .35rem;
  font-size: .78rem;
  color: #9fd4b5;
  padding: .2rem .55rem;
  border-radius: 999px;
  background: var(--accent-soft);
  border: 1px solid rgba(46, 160, 67, 0.35);
}
.online-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 3px rgba(46, 160, 67, 0.2);
}
.user-pill {
  display: inline-flex;
  align-items: center;
  font-size: .78rem;
  color: var(--muted);
  padding: .2rem .55rem;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--panel);
}
.user-pill:empty { display: none; }
.actions { display: flex; gap: .45rem; align-items: center; }
.btn-icon {
  display: inline-flex;
  align-items: center;
  gap: .4rem;
  padding: .45rem .7rem;
}
.btn-icon svg { flex-shrink: 0; }
.nav-toggle { display: none; }
.nav-back { display: none; }

/* —— Tabs / nav —— */
.nav-sheet {
  margin: 0 -1.25rem 0;
}
.nav-sheet-head { display: none; }
.tabs {
  display: flex;
  flex-wrap: nowrap;
  gap: .15rem;
  margin: 0;
  padding: 0 1.25rem;
  border-bottom: 1px solid var(--line);
  overflow-x: auto;
  scrollbar-width: thin;
  background: var(--bg);
  position: sticky;
  top: var(--topbar-h);
  z-index: 25;
}
.tabs button {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: .4rem;
  background: transparent;
  color: var(--muted);
  border: 0;
  border-radius: 0;
  font-weight: 500;
  font-size: .84rem;
  padding: .9rem .7rem .75rem;
  white-space: nowrap;
  flex-shrink: 0;
}
.tabs button svg {
  width: 16px;
  height: 16px;
  opacity: .85;
  flex-shrink: 0;
}
.tabs button .tab-chevron { display: none; }
.tabs button:hover {
  color: var(--text);
  background: transparent;
}
.tabs button.active {
  color: var(--accent);
  background: transparent;
}
.tabs button.active svg { opacity: 1; stroke: var(--accent); }
.tabs button.active::after {
  content: "";
  position: absolute;
  left: .55rem;
  right: .55rem;
  bottom: 0;
  height: 2.5px;
  border-radius: 2px 2px 0 0;
  background: var(--accent);
}
.tabs button.warn { color: #e0a060; }
.nav-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.55);
  z-index: 40;
}

/* —— Content —— */
.content {
  background: transparent;
  border: 0;
  border-radius: 0;
  padding: 1.15rem 0 1.5rem;
  min-height: 40vh;
}
.content:has(> .preview-panel) { padding-top: .85rem; }

.section-head {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  gap: .65rem 1rem;
  margin-bottom: 1rem;
}
.section-head-main {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: .55rem .7rem;
  flex: 1;
  min-width: 0;
}
.section-head h2 {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 700;
  letter-spacing: -.01em;
  display: inline-flex;
  align-items: center;
  gap: .5rem;
}
.section-head h2 .head-ico {
  display: inline-flex;
  width: 28px;
  height: 28px;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  background: var(--accent-soft);
  color: var(--accent);
}
.section-head .api-path {
  font-size: 0.75rem;
  color: var(--muted);
  opacity: 0.9;
}
.section-head-actions {
  display: flex;
  align-items: center;
  gap: .55rem;
  margin-left: auto;
}
.section-desc {
  width: 100%;
  margin: .15rem 0 0;
  color: var(--muted);
  font-size: .9rem;
}
.badge {
  font-size: .72rem;
  padding: .18rem .5rem;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--muted);
  font-weight: 500;
}
.badge.rw {
  color: #9fd4b5;
  border-color: rgba(46, 160, 67, 0.45);
  background: var(--accent-soft);
}

/* —— Cards —— */
.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1.1rem 1.2rem;
  margin-bottom: 1.15rem;
}
.card:last-child { margin-bottom: 0; }
.card h3 { margin: 0 0 .85rem; font-size: .95rem; }
.field-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: .85rem 1rem;
}
.encode-grid .card,
.encode-preview-card,
.encode-page > .json-panel {
  margin-bottom: 0;
}
.check {
  display: flex;
  align-items: center;
  gap: .45rem;
  margin-top: .7rem;
  color: var(--text);
}
.check input { width: auto; margin: 0; }

/* Switch */
.switch {
  display: inline-flex;
  align-items: center;
  gap: .55rem;
  margin: 0;
  color: var(--text);
  font-size: .9rem;
  cursor: pointer;
  user-select: none;
}
.switch input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
  margin: 0;
}
.switch-ui {
  position: relative;
  width: 40px;
  height: 22px;
  border-radius: 999px;
  background: #3a4556;
  border: 1px solid #4a5568;
  flex-shrink: 0;
  transition: background .15s ease, border-color .15s ease;
}
.switch-ui::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #fff;
  transition: transform .15s ease;
}
.switch input:checked + .switch-ui {
  background: var(--accent);
  border-color: var(--accent);
}
.switch input:checked + .switch-ui::after {
  transform: translateX(18px);
}
.switch input:disabled + .switch-ui { opacity: .5; }

.field {
  display: flex;
  flex-direction: column;
  gap: .3rem;
  margin: 0;
  color: var(--muted);
  font-size: .8rem;
}
.field > span:first-child { font-weight: 500; }
.field-control {
  position: relative;
  display: flex;
  align-items: center;
}
.field-control .field-ico {
  position: absolute;
  left: .65rem;
  color: var(--muted);
  pointer-events: none;
  z-index: 1;
}
.field-control select,
.field-control input {
  margin: 0;
  padding-left: 2.1rem;
  background: var(--input);
  border-color: var(--line);
  min-height: 40px;
}
.field-control input[type="number"] { padding-right: 2rem; }

/* —— Encode / Video page —— */
.encode-page {
  display: grid;
  grid-template-columns: 1.1fr 1fr;
  gap: 1rem;
  align-items: start;
  width: 100%;
  min-width: 0;
}
.encode-page > * {
  min-width: 0;
  max-width: 100%;
}
.encode-page > .encode-preview-card { grid-column: 1; grid-row: 2; }
.encode-page > .json-panel { grid-column: 2; grid-row: 2; }
.encode-page > .encode-grid { grid-column: 1 / -1; grid-row: 1; }
.encode-page > .sticky-footer { grid-column: 1 / -1; grid-row: 3; }
.encode-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
  width: 100%;
}
.encode-card { padding: 0; overflow: hidden; }
.encode-card-head {
  display: flex;
  align-items: flex-start;
  gap: .75rem;
  padding: 1rem 1.1rem .85rem;
  border-bottom: 1px solid var(--line-soft);
  cursor: default;
}
.encode-card-head .stream-badge {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: .95rem;
  color: #fff;
  flex-shrink: 0;
}
.encode-card.main .stream-badge { background: var(--blue); }
.encode-card.extra .stream-badge { background: var(--purple); }
.encode-card-titles { flex: 1; min-width: 0; }
.encode-card-titles h3 {
  margin: 0;
  font-size: 1rem;
  font-weight: 650;
  color: var(--text);
}
.encode-card-titles p {
  margin: .2rem 0 0;
  font-size: .78rem;
  color: var(--muted);
}
.encode-card-meta {
  display: flex;
  align-items: center;
  gap: .45rem;
  flex-shrink: 0;
}
.status-on {
  display: inline-flex;
  align-items: center;
  gap: .3rem;
  font-size: .75rem;
  color: #9fd4b5;
}
.status-on::before {
  content: "";
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent);
}
.encode-card-body { padding: 1rem 1.1rem 1.1rem; }
.encode-card .field-row { margin-bottom: .85rem; }
.encode-toggles {
  display: flex;
  flex-wrap: wrap;
  gap: .75rem 1.25rem;
  margin: .25rem 0 1rem;
  padding: .75rem 0 0;
  border-top: 1px solid var(--line-soft);
}
.encode-card-actions {
  display: flex;
  flex-wrap: wrap;
  gap: .55rem;
}
.encode-card-actions .btn-outline-blue { flex: 1; min-width: 140px; }
.encode-card-actions .ghost { flex: 0 0 auto; }

.encode-preview-card .seg {
  display: inline-flex;
  padding: .2rem;
  border-radius: 999px;
  background: var(--input);
  border: 1px solid var(--line);
  gap: .15rem;
  margin-bottom: .75rem;
}
.encode-preview-card .seg button {
  border: 0;
  background: transparent;
  color: var(--muted);
  font-weight: 500;
  font-size: .8rem;
  padding: .4rem .85rem;
  border-radius: 999px;
}
.encode-preview-card .seg button.active {
  background: var(--accent);
  color: #fff;
}
.encode-mini-stage {
  position: relative;
  width: 100%;
  max-width: 100%;
  aspect-ratio: 16 / 9;
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: #0a0c10;
  border: 1px solid var(--line);
}
.encode-mini-stage #preview-canvas,
.encode-mini-stage canvas {
  display: block;
  width: 100% !important;
  max-width: 100%;
  height: 100% !important;
  object-fit: contain;
  background: #0a0c10;
}
.encode-mini-empty {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: .85rem;
  padding: 1rem;
  text-align: center;
  pointer-events: none;
  background: rgba(8, 12, 18, 0.55);
}
.encode-mini-empty[hidden] {
  display: none !important;
}
.json-panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.json-panel-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: .75rem;
  margin-bottom: .65rem;
}
.json-panel-head h3 {
  margin: 0;
  display: flex;
  align-items: center;
  gap: .4rem;
}
.json-panel-head .sub {
  display: block;
  margin-top: .2rem;
  font-size: .75rem;
  color: var(--muted);
  font-weight: 400;
}
.json-panel textarea {
  flex: 1;
  min-height: 240px;
  margin: 0;
  border-radius: var(--radius-sm);
}

.sticky-footer {
  position: sticky;
  bottom: 0;
  z-index: 20;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: .65rem;
  margin: 1rem -1.25rem 0;
  padding: .75rem 1.25rem;
  background: rgba(13, 17, 23, 0.94);
  backdrop-filter: blur(10px);
  border-top: 1px solid var(--line);
}
.sticky-footer .btn-save {
  padding: .7rem 1.35rem;
  font-size: .95rem;
  box-shadow: 0 4px 16px rgba(46, 160, 67, 0.25);
}
.sticky-footer .footer-status {
  margin-left: auto;
  font-size: .82rem;
  color: #9fd4b5;
  display: inline-flex;
  align-items: center;
  gap: .4rem;
}
.sticky-footer .footer-status.err { color: #f0b4b4; }

/* —— Preview tab —— */
.preview-panel {
  background: transparent;
  border: 0;
  border-radius: 0;
  padding: 0;
  margin: 0;
}
.preview-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: .6rem 1rem;
  margin-bottom: .65rem;
}
.preview-stream-toggle {
  display: inline-flex;
  gap: .3rem;
  margin-left: auto;
}
.preview-stream-toggle .ghost {
  padding: .35rem .7rem;
  font-size: .82rem;
}
.preview-stream-toggle .ghost.active {
  border-color: var(--accent);
  color: #b7e0c7;
  background: var(--accent-soft);
}
.preview-stats-btn {
  padding: .35rem .7rem;
  font-size: .82rem;
}
.preview-stats-btn[aria-pressed="true"] {
  border-color: var(--accent);
  color: #b7e0c7;
  background: var(--accent-soft);
}
.preview-stage {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 9;
  max-height: min(56.25vw, 560px);
  background: #0a0c10;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  overflow: hidden;
}
#preview-canvas {
  display: block;
  width: 100% !important;
  max-width: 100%;
  height: 100% !important;
  object-fit: contain;
  background: #0a0c10;
}
.preview-loading {
  position: absolute;
  inset: 0;
  z-index: 3;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: .75rem;
  background: rgba(8, 12, 18, .62);
  color: var(--text);
  font-size: .9rem;
}
.preview-loading[hidden] { display: none !important; }
.preview-spinner {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  border: 3px solid rgba(232, 238, 246, .18);
  border-top-color: var(--accent);
  animation: preview-spin .75s linear infinite;
}
@keyframes preview-spin { to { transform: rotate(360deg); } }
.preview-stats-overlay {
  position: absolute;
  left: .65rem;
  top: .65rem;
  z-index: 2;
  display: none;
  grid-template-columns: 1fr;
  gap: .2rem .85rem;
  min-width: 220px;
  max-width: min(320px, calc(100% - 1.3rem));
  max-height: calc(100% - 1.3rem);
  overflow: auto;
  padding: .65rem .75rem;
  border-radius: var(--radius-sm);
  border: 1px solid rgba(120, 140, 160, .35);
  background: rgba(8, 12, 18, .78);
  backdrop-filter: blur(4px);
  pointer-events: none;
}
.preview-stats-overlay.is-open { display: grid; }
.preview-stats-overlay[hidden] { display: none !important; }
.preview-stats-overlay .stat {
  display: grid;
  grid-template-columns: 5.5rem 1fr;
  gap: .35rem;
  align-items: baseline;
  background: transparent;
  border: 0;
  padding: 0;
}
.preview-stats-overlay .lbl {
  display: block;
  color: var(--muted);
  font-size: .72rem;
  margin: 0;
}
.preview-stats-overlay .val {
  font-family: var(--mono);
  font-size: .78rem;
  color: #e8eef6;
  word-break: break-all;
}
.preview-status { margin: .5rem 0 0; font-size: .85rem; }
.preview-status.err { color: var(--danger); }

/* —— Tables / misc —— */
.ro-table { width: 100%; border-collapse: collapse; font-size: .9rem; }
.ro-table th, .ro-table td {
  text-align: left;
  padding: .35rem .4rem;
  border-bottom: 1px solid var(--line);
  vertical-align: top;
}
.ro-table th { color: var(--muted); font-weight: 500; width: 38%; }
.ro-table td { font-family: var(--mono); font-size: 12.5px; word-break: break-all; }

.footer-actions {
  display: flex;
  gap: .6rem;
  margin-top: .85rem;
  flex-wrap: wrap;
}
.json-err { color: var(--danger); font-size: .85rem; margin: .4rem 0 0; }

.cap-stack { display: flex; flex-direction: column; gap: 1.15rem; margin-bottom: 1rem; }
.cap-summary {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: .55rem;
  margin-bottom: .25rem;
}
.stat {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: .65rem .75rem;
}
.stat .lbl { display: block; color: var(--muted); font-size: .75rem; margin-bottom: .2rem; }
.stat .val { font-family: var(--mono); font-size: .9rem; }

.chip-grid { display: flex; flex-wrap: wrap; gap: .35rem; }
.chip {
  font-size: .78rem;
  padding: .2rem .5rem;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--muted);
  background: transparent;
}
.chip.on {
  color: #b7e0c7;
  border-color: var(--accent-dim);
  background: var(--accent-soft);
}
.chip.off { opacity: .55; }
.chip.tag { color: #c9d4e3; border-color: #3a4658; }

.stream-table { width: 100%; border-collapse: collapse; font-size: .82rem; }
.stream-table th, .stream-table td {
  text-align: left;
  padding: .4rem .45rem;
  border-bottom: 1px solid var(--line);
  vertical-align: top;
}
.stream-table th { color: var(--muted); font-weight: 500; }
.stream-table td { font-family: var(--mono); font-size: 12px; }

details.raw-json {
  margin-top: .75rem;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: .4rem .7rem .7rem;
  background: var(--panel);
}
details.raw-json summary {
  cursor: pointer;
  color: var(--muted);
  font-size: .85rem;
  padding: .35rem 0;
  list-style: none;
  display: flex;
  align-items: center;
  gap: .4rem;
}
details.raw-json summary::-webkit-details-marker { display: none; }
details.raw-json textarea { margin-top: .4rem; }

.cam-toggles {
  display: flex;
  flex-wrap: wrap;
  gap: .55rem 1.25rem;
  margin-bottom: 1rem;
}
.cam-toggles .check { margin-top: 0; }
.cam-slider {
  display: grid;
  grid-template-columns: minmax(140px, 1.1fr) 1fr auto;
  align-items: center;
  gap: .55rem;
  margin-bottom: .85rem;
  color: var(--muted);
  font-size: .9rem;
}
.cam-slider input[type="range"] { width: 100%; }
.cam-slider-val {
  font-family: var(--mono);
  color: var(--text);
  min-width: 2rem;
  text-align: right;
}
.cam-select {
  display: flex;
  flex-direction: column;
  gap: .35rem;
  margin-bottom: .85rem;
  color: var(--muted);
  font-size: .9rem;
}
.cam-select select { max-width: 280px; }

.motion-card { margin-bottom: .75rem; }
.motion-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 1rem 1.5rem;
  margin-bottom: .75rem;
}
.motion-level {
  display: flex;
  align-items: center;
  gap: .55rem;
  color: var(--muted);
  font-size: .9rem;
}
.motion-level input[type="range"] { width: 140px; }
.motion-level span {
  font-family: var(--mono);
  color: var(--text);
  min-width: 1.2rem;
}
.motion-reactions {
  display: flex;
  flex-wrap: wrap;
  gap: .35rem 1rem;
  margin-bottom: .85rem;
}
.motion-reactions .check { margin-top: 0; }
.motion-stage {
  position: relative;
  width: 100%;
  max-width: 960px;
  aspect-ratio: 16 / 9;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: #121820;
  user-select: none;
  touch-action: none;
}
.motion-bg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  opacity: 0.9;
}
.motion-grid {
  position: absolute;
  inset: 0;
  display: grid;
  z-index: 1;
}
.motion-cell {
  margin: 0;
  padding: 0;
  border: 1px solid rgba(255, 255, 255, 0.06);
  background: transparent;
  cursor: crosshair;
  min-width: 0;
  min-height: 0;
}
.motion-cell:disabled { cursor: default; }
.motion-cell.on {
  background: rgba(220, 70, 70, 0.45);
  border-color: rgba(255, 120, 120, 0.35);
}
.motion-cell:hover:not(:disabled) {
  outline: 1px solid rgba(255, 255, 255, 0.35);
  outline-offset: -1px;
  z-index: 2;
}
.motion-tools {
  display: flex;
  flex-wrap: wrap;
  gap: .45rem;
  margin-top: .7rem;
}

.preview-alarm {
  margin-top: .55rem;
  padding: .55rem .75rem;
  border-radius: var(--radius-sm);
  border: 1px solid rgba(220, 80, 80, 0.45);
  background: rgba(160, 40, 40, 0.35);
  color: #ffd0d0;
  font-size: .9rem;
  animation: alarm-pulse 1.2s ease-in-out infinite;
}
@keyframes alarm-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: .72; }
}
.feed-open-btn {
  position: fixed;
  right: 1rem;
  bottom: 1rem;
  z-index: 41;
  padding: .55rem .9rem;
  font-size: .85rem;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 999px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.35);
}
.feed-open-btn:hover { border-color: var(--accent); color: #b7e0c7; }
.feed-open-btn[hidden] { display: none !important; }
.alarm-feed {
  position: fixed;
  right: 1rem;
  bottom: 1rem;
  width: min(380px, calc(100vw - 2rem));
  max-height: min(52vh, 420px);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.35);
  z-index: 40;
}
.alarm-feed[hidden] { display: none !important; }
.alarm-feed-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: .5rem;
  padding: .55rem .65rem;
  border-bottom: 1px solid var(--line);
  font-size: .85rem;
}
.alarm-feed-head-actions {
  display: flex;
  align-items: center;
  gap: .35rem;
}
.feed-close {
  width: 28px;
  height: 28px;
  padding: 0;
  font-size: 1.15rem;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
#alarm-feed-list {
  list-style: none;
  margin: 0;
  padding: .45rem;
  overflow-y: auto;
  flex: 1;
  font-size: .8rem;
  font-family: var(--mono);
}
#alarm-feed-list li {
  padding: .45rem .55rem;
  border-radius: 6px;
  margin-bottom: .4rem;
  background: var(--input);
  border: 1px solid transparent;
  line-height: 1.35;
}
#alarm-feed-list li.motion {
  border-color: rgba(220, 80, 80, 0.4);
  background: rgba(140, 40, 40, 0.25);
}
#alarm-feed-list li .meta {
  display: block;
  color: var(--muted);
  font-size: .72rem;
  margin-top: .3rem;
}
.logs-page { display: flex; flex-direction: column; gap: 1.15rem; }
.logs-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: .65rem 1rem;
}
.logs-by-day {
  display: flex;
  flex-direction: column;
  gap: .75rem;
}
.logs-day {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}
.logs-day-title {
  margin: 0;
  padding: .85rem 1rem;
  font-size: .95rem;
  font-weight: 650;
  color: var(--text);
  letter-spacing: -.01em;
  cursor: pointer;
  list-style: none;
  display: flex;
  align-items: center;
  gap: .55rem;
  user-select: none;
}
.logs-day-title::-webkit-details-marker { display: none; }
.logs-day-chevron {
  width: 0.45rem;
  height: 0.45rem;
  border-right: 2px solid var(--muted);
  border-bottom: 2px solid var(--muted);
  transform: rotate(-45deg);
  transition: transform .15s ease;
  flex-shrink: 0;
}
.logs-day[open] .logs-day-chevron {
  transform: rotate(45deg);
}
.logs-day .logs-list {
  padding: 0 .85rem 1rem;
  border-top: 1px solid var(--line-soft);
  padding-top: .75rem;
}
.logs-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: .5rem;
}
.logs-list li {
  padding: .7rem .85rem;
  border-radius: var(--radius-sm);
  background: var(--panel);
  border: 1px solid var(--line);
  font-family: var(--mono);
  font-size: .84rem;
  line-height: 1.4;
}
.logs-list li.motion {
  border-color: rgba(220, 80, 80, 0.4);
  background: rgba(140, 40, 40, 0.18);
}
.logs-list .log-type {
  display: block;
  color: var(--text);
  font-weight: 600;
}
.logs-list .meta {
  display: block;
  color: var(--muted);
  font-size: .78rem;
  margin-top: .3rem;
}

/* —— Mobile —— */
@media (max-width: 900px) {
  .encode-page {
    display: flex;
    flex-direction: column;
  }
  .encode-page > .encode-preview-card,
  .encode-page > .json-panel,
  .encode-page > .encode-grid,
  .encode-page > .sticky-footer {
    grid-column: auto;
    grid-row: auto;
  }
  .encode-grid { grid-template-columns: 1fr; }
  .field-row { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 720px) {
  html, body {
    width: 100%;
    max-width: 100%;
    overflow-x: hidden;
  }
  #app {
    width: 100%;
    max-width: 100%;
    margin: 0;
    padding: 0 .75rem 5.5rem;
    overflow-x: hidden;
  }
  .content {
    width: 100%;
    max-width: 100%;
    min-width: 0;
  }
  .topbar {
    margin: 0 -.75rem;
    padding: .55rem .75rem;
    width: auto;
  }
  .btn-label { display: none; }
  .nav-toggle { display: inline-flex; }
  .nav-back { display: inline-flex; }
  .nav-back[hidden] { display: none !important; }
  .user-pill { display: none; }

  .nav-sheet {
    position: fixed;
    inset: 0;
    z-index: 50;
    margin: 0;
    display: flex;
    flex-direction: column;
    background: var(--bg);
    transform: translateY(105%);
    transition: transform .22s ease;
    pointer-events: none;
  }
  .nav-sheet.is-open {
    transform: translateY(0);
    pointer-events: auto;
  }
  .nav-sheet-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: .5rem;
    padding: .65rem .75rem;
    padding-top: calc(.65rem + env(safe-area-inset-top));
    border-bottom: 1px solid var(--line);
    background: var(--bg);
    flex-shrink: 0;
  }
  .nav-sheet-title {
    font-size: 1.15rem;
    font-weight: 700;
  }
  .tabs {
    position: static;
    top: auto;
    flex: 1;
    flex-direction: column;
    flex-wrap: nowrap;
    align-items: stretch;
    gap: .55rem;
    overflow-y: auto;
    overflow-x: hidden;
    border-bottom: 0;
    background: transparent;
    padding: .85rem .75rem calc(1.25rem + env(safe-area-inset-bottom));
    transform: none;
    box-shadow: none;
  }
  .tabs button {
    justify-content: flex-start;
    gap: .75rem;
    padding: .95rem 1rem;
    border-radius: 14px;
    font-size: 1rem;
    font-weight: 600;
    color: var(--text);
    background: var(--panel);
    border: 1px solid var(--line);
  }
  .tabs button svg {
    width: 20px;
    height: 20px;
    opacity: 1;
  }
  .tabs button .tab-label { flex: 1; text-align: left; }
  .tabs button .tab-chevron {
    display: inline-block;
    width: .45rem;
    height: .45rem;
    border-right: 2px solid var(--muted);
    border-bottom: 2px solid var(--muted);
    transform: rotate(-45deg);
    opacity: .7;
  }
  .tabs button.active {
    color: var(--accent);
    background: var(--accent-soft);
    border-color: rgba(46, 160, 67, 0.55);
    box-shadow: 0 0 0 1px rgba(46, 160, 67, 0.2);
  }
  .tabs button.active svg { stroke: var(--accent); }
  .tabs button.active .tab-chevron {
    border-color: var(--accent);
  }
  .tabs button.active::after { display: none; }
  body.nav-open { overflow: hidden; }
  .nav-backdrop:not([hidden]) { display: block; }

  .section-head h2 { font-size: 1.2rem; }
  .section-head-actions { display: none; }
  .section-desc { font-size: .88rem; }

  /* Video page: preview first, JSON last, accordion streams */
  .encode-page {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    width: 100%;
  }
  .encode-page > .encode-preview-card,
  .encode-page > .encode-grid,
  .encode-page > .json-panel,
  .encode-page > .sticky-footer {
    width: 100%;
    max-width: 100%;
    flex: 0 0 auto;
  }
  .encode-page > .encode-preview-card { order: 1; }
  .encode-page > .encode-grid { order: 2; }
  .encode-page > .json-panel { order: 3; }
  .encode-page > .sticky-footer { order: 4; }
  .encode-preview-card {
    width: 100%;
    box-sizing: border-box;
  }
  .encode-preview-card h3 { display: none; }
  .encode-preview-card .seg {
    width: 100%;
    display: grid;
    grid-template-columns: 1fr 1fr;
    margin-bottom: .75rem;
  }
  .encode-preview-card .seg button {
    text-align: center;
    padding: .55rem .4rem;
  }
  .encode-grid {
    gap: .75rem;
    width: 100%;
    grid-template-columns: 1fr;
  }
  .encode-card {
    padding: 0;
    width: 100%;
  }
  .field-row { grid-template-columns: 1fr; }
  .encode-card-head {
    cursor: pointer;
  }
  .encode-card.is-collapsed .encode-card-body {
    display: none;
  }
  .encode-card-meta .encode-chevron {
    display: inline-block;
    width: .45rem;
    height: .45rem;
    margin-left: .35rem;
    border-right: 2px solid var(--muted);
    border-bottom: 2px solid var(--muted);
    transform: rotate(45deg);
    transition: transform .15s ease;
  }
  .encode-card.is-collapsed .encode-chevron {
    transform: rotate(-45deg);
  }
  .sticky-footer {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    margin: 0;
    padding: .65rem .75rem calc(.65rem + env(safe-area-inset-bottom));
    justify-content: stretch;
  }
  .sticky-footer .footer-status { display: none; }
  .sticky-footer .ghost { display: none; }
  .sticky-footer .btn-save { flex: 1; width: 100%; }
  .json-panel.mobile-collapse {
    padding: 0;
    overflow: hidden;
  }
  .json-panel.mobile-collapse > .json-toggle {
    width: 100%;
    border: 0;
    border-radius: 0;
    background: transparent;
    text-align: left;
    padding: .9rem 1rem;
    font-weight: 650;
  }
  .json-panel.mobile-collapse > .json-panel-head,
  .json-panel.mobile-collapse > .json-body { display: none; }
  .json-panel.mobile-collapse.is-open > .json-panel-head,
  .json-panel.mobile-collapse.is-open > .json-body { display: block; }
  .json-panel.mobile-collapse.is-open > .json-body {
    padding: 0 .85rem .85rem;
  }
  .alarm-feed {
    right: .5rem;
    bottom: calc(var(--footer-h) + .5rem);
  }
  .feed-open-btn {
    bottom: calc(var(--footer-h) + .5rem);
  }
}
@media (max-width: 420px) {
  .field-row { grid-template-columns: 1fr; }
}
