:root {
  color-scheme: dark;
  --bg: #090b0e;
  --panel: #11151a;
  --panel-soft: #171c22;
  --text: #f4f6f8;
  --muted: #8b96a3;
  --line: rgba(255, 255, 255, 0.09);
  --accent: #55e38f;
  --accent-soft: rgba(85, 227, 143, 0.17);
  --danger: #ff6b6b;
  font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; min-height: 100%; background: var(--bg); color: var(--text); }

body {
  padding: 28px clamp(16px, 3vw, 48px) 22px;
  background:
    radial-gradient(circle at 84% -10%, rgba(55, 113, 84, 0.22), transparent 30rem),
    var(--bg);
}

button, input, select, textarea { font: inherit; }
button { color: inherit; }

.composite-source {
  position: fixed;
  width: 2px;
  height: 2px;
  left: -20px;
  top: -20px;
  opacity: 0.01;
  pointer-events: none;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  max-width: 1800px;
  margin: 0 auto 24px;
}

.brand { flex: 0 0 auto; }

.header-actions,
.account-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 9px;
  flex-wrap: wrap;
}

.account-actions > span {
  margin: 0 4px;
  color: var(--muted);
  font-size: 13px;
}

.button {
  min-height: 38px;
  padding: 0 13px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--panel-soft);
  cursor: pointer;
  font-size: 12px;
  font-weight: 700;
}

.button:hover { border-color: rgba(85, 227, 143, 0.45); background: var(--accent-soft); }
.button.primary { border-color: transparent; color: #06120b; background: var(--accent); }
.button.primary:hover { background: #75efa5; }
.button.danger { color: #ffb1b1; background: rgba(255, 107, 107, 0.08); }
.button:disabled { opacity: .38; cursor: not-allowed; }
.button.wide { width: 100%; }

body[data-admin="false"] .admin-only { display: none; }
body[data-authenticated="false"] .zoom-controls,
body[data-authenticated="false"] .ptz-panel { display: none; }
body[data-authenticated="false"] canvas { cursor: default; }

.eyebrow {
  margin: 0 0 7px;
  color: var(--accent);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.16em;
}

h1 { margin: 0; font-size: clamp(28px, 4vw, 48px); line-height: 1; letter-spacing: -0.04em; }

.connection {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 38px;
  padding: 0 15px;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--muted);
  background: rgba(17, 21, 26, 0.8);
  font-size: 13px;
  white-space: nowrap;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #e5a84b;
  box-shadow: 0 0 12px currentColor;
}

.connection[data-state="streaming"] { color: var(--text); }
.connection[data-state="streaming"] .status-dot { background: var(--accent); }
.connection[data-state="error"] .status-dot,
.connection[data-state="reconnecting"] .status-dot { background: var(--danger); }

.camera-list {
  display: grid;
  gap: 38px;
  max-width: 1800px;
  margin: 0 auto;
}

.camera-section { min-width: 0; }

.camera-section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin: 0 0 13px;
}

.camera-title {
  margin: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: clamp(19px, 2.2vw, 27px);
  letter-spacing: -.025em;
  white-space: nowrap;
}

.lens-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 18px;
}

.empty-state {
  margin: 70px 0;
  color: var(--muted);
  text-align: center;
}

.camera-card {
  min-width: 0;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 18px;
  outline: none;
  background: var(--panel);
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.22);
}

.camera-card:focus-visible { border-color: var(--accent); }

.viewport {
  position: relative;
  aspect-ratio: 16 / 9;
  overflow: hidden;
  background: #020303;
  touch-action: none;
}

canvas { display: block; width: 100%; height: 100%; cursor: zoom-in; }
.camera-card[data-zoomed="true"] canvas { cursor: grab; }
.camera-card[data-dragging="true"] canvas { cursor: grabbing; }

.stream-badge,
.icon-button,
.ptz-panel,
.toolbar {
  transition: opacity 160ms ease, transform 160ms ease;
}

.stream-badge {
  position: absolute;
  top: 14px;
  left: 14px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 10px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 8px;
  background: rgba(2, 3, 3, 0.7);
  backdrop-filter: blur(10px);
  font-size: 11px;
  letter-spacing: .03em;
}

.stream-badge strong { color: var(--accent); font-size: 10px; }
.stream-badge span { color: #d7dde3; }

.icon-button {
  position: absolute;
  top: 13px;
  right: 13px;
  display: grid;
  width: 38px;
  height: 38px;
  place-items: center;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 9px;
  color: white;
  background: rgba(2, 3, 3, 0.7);
  backdrop-filter: blur(10px);
  cursor: pointer;
}

.icon-button:hover { background: rgba(85, 227, 143, 0.22); }
.icon-button svg { width: 19px; fill: none; stroke: currentColor; stroke-width: 1.8; }

.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  min-height: 68px;
  padding: 12px 14px 12px 18px;
  border-top: 1px solid var(--line);
}

.camera-name { display: flex; flex-direction: column; gap: 3px; }
.camera-name span { font-weight: 720; font-size: 15px; }
.camera-name small { color: var(--muted); font-size: 11px; }
.camera-name .accent { color: var(--accent); }

.zoom-controls {
  display: grid;
  grid-template-columns: 38px 58px 38px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 10px;
}

.zoom-controls button {
  height: 36px;
  border: 0;
  border-right: 1px solid var(--line);
  color: var(--text);
  background: var(--panel-soft);
  cursor: pointer;
}

.zoom-controls button:last-child { border-right: 0; }
.zoom-controls button:hover { background: var(--accent-soft); }
.zoom-controls .zoom-value { color: var(--muted); font-size: 11px; }

.ptz-panel {
  position: absolute;
  right: 16px;
  bottom: 16px;
  display: grid;
  grid-template-columns: repeat(3, 40px);
  grid-template-rows: repeat(3, 40px);
  gap: 4px;
  padding: 6px;
  border: 1px solid rgba(255, 255, 255, 0.13);
  border-radius: 16px;
  background: rgba(2, 3, 3, 0.7);
  backdrop-filter: blur(12px);
}

.ptz-panel button {
  border: 0;
  border-radius: 9px;
  color: white;
  background: rgba(255, 255, 255, 0.08);
  cursor: pointer;
  user-select: none;
}

.ptz-panel button:hover,
.ptz-panel button:active { color: #07120c; background: var(--accent); }
.ptz-panel [data-direction="up"] { grid-column: 2; grid-row: 1; }
.ptz-panel [data-direction="left"] { grid-column: 1; grid-row: 2; }
.ptz-panel [data-direction="right"] { grid-column: 3; grid-row: 2; }
.ptz-panel [data-direction="down"] { grid-column: 2; grid-row: 3; }

.ptz-center {
  grid-column: 2;
  grid-row: 2;
  align-self: center;
  justify-self: center;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 14px var(--accent);
}

.waiting {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  color: var(--muted);
  background: #07090b;
  font-size: 13px;
  pointer-events: none;
}

.camera-section.has-video .waiting { display: none; }

.spinner {
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255, 255, 255, 0.14);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 800ms linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  z-index: 20;
  max-width: min(90vw, 560px);
  padding: 11px 15px;
  border: 1px solid rgba(255, 107, 107, 0.4);
  border-radius: 10px;
  color: #fff;
  background: rgba(72, 19, 23, 0.94);
  opacity: 0;
  transform: translate(-50%, 12px);
  pointer-events: none;
  transition: 180ms ease;
}

.toast.visible { opacity: 1; transform: translate(-50%, 0); }

dialog {
  width: min(92vw, 430px);
  padding: 0;
  border: 1px solid rgba(255, 255, 255, 0.13);
  border-radius: 18px;
  color: var(--text);
  background: var(--panel);
  box-shadow: 0 28px 90px rgba(0, 0, 0, .65);
}

dialog::backdrop { background: rgba(0, 0, 0, .72); backdrop-filter: blur(5px); }
.admin-dialog { width: min(94vw, 1050px); }
.dialog-form, .admin-shell { padding: 24px; }
.dialog-form, .compact-form { display: grid; gap: 14px; }

.dialog-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 8px;
}

.dialog-heading h2 { margin: 0; font-size: 25px; letter-spacing: -.025em; }
.dialog-heading .eyebrow { margin-bottom: 5px; }

.close-button {
  width: 35px;
  height: 35px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 9px;
  color: var(--muted);
  background: var(--panel-soft);
  cursor: pointer;
  font-size: 24px;
  line-height: 1;
}

.dialog-form label,
.compact-form label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
}

input:not([type="checkbox"]), textarea {
  width: 100%;
  min-height: 42px;
  padding: 9px 11px;
  border: 1px solid var(--line);
  border-radius: 9px;
  outline: none;
  color: var(--text);
  background: #0c1014;
}

textarea { resize: vertical; }
input:focus, textarea:focus, select:focus { border-color: rgba(85, 227, 143, .7); }
.compact-form .checkbox { display: flex; align-items: center; gap: 8px; }
.checkbox input { accent-color: var(--accent); }
.form-hint, .form-error { min-height: 14px; margin: -5px 0 0; font-size: 11px; }
.form-hint { color: var(--muted); }
.form-error { color: #ff9c9c; }

.admin-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 24px;
}

.settings-section { grid-column: 1 / -1; }
.settings-form { grid-template-columns: minmax(0, 1fr) auto; align-items: end; }
.settings-form .form-error { grid-column: 1 / -1; }

.admin-section {
  min-width: 0;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: rgba(0, 0, 0, .1);
}

.admin-section h3 { margin: 0 0 16px; font-size: 17px; }
.entity-list { display: grid; gap: 8px; margin-top: 18px; }
.entity-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-width: 0;
  padding: 10px 11px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel-soft);
}

.entity-row > div:first-child { display: grid; gap: 3px; min-width: 0; }
.entity-row strong, .entity-row small { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.entity-row strong { font-size: 13px; }
.entity-row small { color: var(--muted); font-size: 10px; }
.entity-row .button { flex: 0 0 auto; min-height: 32px; }
.entity-actions { display: flex; align-items: center; justify-content: flex-end; gap: 6px; }

.camera-card:fullscreen {
  position: relative;
  width: 100vw;
  height: 100vh;
  border: 0;
  border-radius: 0;
  background: black;
}

.camera-card:fullscreen .viewport { width: 100%; height: 100%; aspect-ratio: auto; }
.camera-card:fullscreen .toolbar {
  position: absolute;
  left: 18px;
  right: 18px;
  bottom: 18px;
  z-index: 3;
  width: auto;
  max-width: 520px;
  border: 1px solid rgba(255, 255, 255, 0.13);
  border-radius: 14px;
  background: rgba(2, 3, 3, 0.72);
  backdrop-filter: blur(14px);
}

.camera-card:fullscreen .ptz-panel { bottom: 104px; }

@media (max-width: 1200px) {
  .lens-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .lens-grid[data-count="3"] .camera-card:last-child { grid-column: 1 / -1; }
  .app-header { align-items: flex-start; }
  .header-actions { max-width: 720px; }
}

@media (max-width: 760px) {
  body { padding: 20px 12px; }
  .lens-grid { grid-template-columns: 1fr; }
  .lens-grid[data-count="3"] .camera-card:last-child { grid-column: auto; }
  .app-header { align-items: flex-start; flex-direction: column; gap: 14px; }
  .header-actions { width: 100%; justify-content: flex-start; }
  .camera-section-header { align-items: flex-start; flex-direction: column; }
  .admin-grid { grid-template-columns: 1fr; }
  .settings-form { grid-template-columns: 1fr; }
  .settings-form .form-error { grid-column: auto; }
}

@media (max-width: 520px) {
  .stream-badge span { display: none; }
  .ptz-panel { transform: scale(.88); transform-origin: right bottom; }
  .account-actions { justify-content: flex-start; }
  .entity-row { align-items: flex-start; flex-direction: column; }
  .entity-actions { width: 100%; justify-content: flex-start; }
  .dialog-form, .admin-shell { padding: 18px; }
  .admin-section { padding: 14px; }
}
