:root {
  color-scheme: dark;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: #0e1214;
  color: #eef4ef;
  --hud-bg: rgba(13, 17, 18, 0.58);
  --hud-bg-strong: rgba(13, 17, 18, 0.76);
  --hud-border: rgba(231, 242, 239, 0.12);
  --hud-soft: rgba(231, 242, 239, 0.075);
  --hud-control: rgba(27, 34, 34, 0.82);
  --hud-control-hover: rgba(38, 48, 48, 0.9);
  --hud-muted: #9aa8a4;
  --hud-text: #eef4ef;
  --hud-accent: #a8e6cf;
  --hud-accent-strong: #d9f07f;
}

* {
  box-sizing: border-box;
}

html,
body,
.app {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
}

body {
  background: #0e1214;
}

[hidden] {
  display: none !important;
}

.app {
  position: relative;
}

.viewer-stage {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #8fc0df;
}

.viewer-stage.is-split {
  display: grid;
  gap: 4px;
  padding: 4px;
  background: #0b0f10;
}

.viewer-stage.is-split[data-layout="1"] {
  grid-template-columns: minmax(0, 1fr);
}

.viewer-stage.is-split[data-layout="2"] {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.viewer-stage.is-split[data-layout="4"] {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.viewer-stage.is-split[data-layout="6"],
.viewer-stage.is-split[data-layout="9"] {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.viewer-frame {
  display: block;
  width: 100%;
  height: 100%;
  border: 0;
  background: #8fc0df;
  touch-action: none;
  user-select: none;
}

.viewer-tile {
  position: relative;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  border: 1px solid rgba(238, 244, 239, 0.1);
  border-radius: 5px;
  background: #0e1214;
}

.viewer-tile.is-active {
  border-color: rgba(168, 230, 207, 0.9);
  box-shadow: inset 0 0 0 1px rgba(168, 230, 207, 0.42);
}

.viewer-tile:focus-visible {
  outline: 2px solid #8fd7ff;
  outline-offset: -3px;
}

.viewer-tile-label {
  position: absolute;
  top: 6px;
  left: 6px;
  z-index: 1;
  max-width: calc(100% - 12px);
  overflow: hidden;
  padding: 4px 7px;
  border-radius: 5px;
  background: rgba(8, 11, 12, 0.58);
  color: var(--hud-text);
  font-size: 11px;
  font-weight: 700;
  line-height: 1;
  pointer-events: none;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.panel {
  position: absolute;
  top: 12px;
  left: 12px;
  z-index: 2;
  width: min(292px, calc(100vw - 24px));
  display: grid;
  gap: 8px;
  padding: 10px;
  border: 1px solid var(--hud-border);
  border-radius: 7px;
  background: var(--hud-bg);
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.22);
  backdrop-filter: blur(10px);
  pointer-events: none;
  transition: background-color 140ms ease, border-color 140ms ease;
}

.panel :is(a, button, input, select, textarea, label) {
  pointer-events: auto;
}

.panel:hover,
.panel:focus-within {
  border-color: rgba(231, 242, 239, 0.18);
  background: var(--hud-bg-strong);
}

:focus-visible {
  outline: 2px solid #8fd7ff;
  outline-offset: 3px;
}

.brand {
  display: flex;
  min-width: 0;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.brand-actions {
  display: flex;
  flex: 0 0 auto;
  gap: 5px;
}

h1 {
  margin: 0;
  font-size: 15px;
  line-height: 1.1;
  letter-spacing: 0;
}

p {
  margin: 2px 0 0;
  color: var(--hud-muted);
  font-size: 11px;
  line-height: 1.2;
}

a,
button,
input,
select {
  min-height: 30px;
  border: 1px solid var(--hud-border);
  border-radius: 5px;
  background: var(--hud-control);
  color: var(--hud-text);
  font: inherit;
}

a,
button {
  display: grid;
  place-items: center;
  text-decoration: none;
}

button {
  cursor: pointer;
  transition: background-color 140ms ease, border-color 140ms ease, color 140ms ease;
}

button:hover,
a:hover,
select:hover,
input:hover {
  border-color: rgba(168, 230, 207, 0.38);
  background: var(--hud-control-hover);
}

button:disabled,
input:disabled,
select:disabled {
  cursor: not-allowed;
  opacity: 0.62;
}

.icon-button {
  width: 30px;
  padding: 0;
  font-weight: 700;
  font-size: 12px;
}

.icon-button[aria-pressed="true"] {
  border-color: rgba(217, 240, 127, 0.68);
  background: rgba(217, 240, 127, 0.92);
  color: #111711;
}

.field {
  display: grid;
  gap: 4px;
  font-size: 11px;
  color: var(--hud-muted);
}

select {
  width: 100%;
  min-width: 0;
  overflow: hidden;
  padding: 0 8px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

input {
  width: 100%;
  min-width: 0;
  padding: 0 8px;
}

.stats {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 5px;
}

.stats > div {
  min-width: 0;
  padding: 6px;
  border-radius: 5px;
  background: var(--hud-soft);
}

.stats span {
  display: block;
  color: var(--hud-muted);
  font-size: 9px;
  line-height: 1.1;
}

.stats strong {
  display: block;
  overflow: hidden;
  margin-top: 2px;
  font-size: 12px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

button {
  padding: 0 10px;
}

.toolbar {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 6px;
}

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

.admin-split-controls {
  display: grid;
  grid-template-columns: minmax(86px, 0.65fr) minmax(0, 1.35fr);
  gap: 6px;
}

.admin-controls .field {
  min-width: 0;
}

.admin-panel-controls {
  display: grid;
  gap: 8px;
}

.admin-mobile-toggle {
  display: none;
}

.camera-list {
  min-height: 30px;
  max-height: 72px;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 4px;
  overflow-y: auto;
  scrollbar-gutter: stable;
}

.camera-toggle {
  min-width: 0;
  min-height: 28px;
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 0 7px;
  border: 1px solid var(--hud-border);
  border-radius: 5px;
  background: rgba(27, 34, 34, 0.72);
  color: var(--hud-text);
  cursor: pointer;
}

.camera-toggle.is-active {
  border-color: rgba(168, 230, 207, 0.68);
  background: rgba(168, 230, 207, 0.12);
}

.camera-toggle.is-disabled {
  cursor: not-allowed;
  opacity: 0.62;
}

.camera-toggle input {
  width: 13px;
  min-width: 13px;
  min-height: 13px;
  margin: 0;
}

.camera-toggle span,
.camera-empty {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.camera-toggle span {
  font-size: 11px;
}

.camera-empty {
  grid-column: 1 / -1;
  min-height: 30px;
  display: grid;
  align-items: center;
  padding: 0 8px;
  border-radius: 5px;
  background: var(--hud-soft);
  color: var(--hud-muted);
  font-size: 11px;
}

.chat-form button,
.admin-login button {
  border-color: rgba(217, 240, 127, 0.5);
  background: rgba(217, 240, 127, 0.9);
  color: #111711;
  font-weight: 700;
}

.toolbar button {
  background: rgba(27, 34, 34, 0.76);
  color: var(--hud-text);
  font-weight: 700;
}

.toolbar button:hover {
  border-color: rgba(168, 230, 207, 0.5);
  background: rgba(168, 230, 207, 0.14);
}

.admin-chat {
  display: grid;
  gap: 6px;
}

.admin-chat summary {
  min-height: 30px;
  display: grid;
  place-items: center;
  border: 1px solid var(--hud-border);
  border-radius: 5px;
  background: rgba(27, 34, 34, 0.76);
  color: var(--hud-text);
  cursor: pointer;
  font-weight: 700;
  list-style: none;
}

.admin-chat summary::-webkit-details-marker {
  display: none;
}

.chat-form {
  display: grid;
  gap: 6px;
}

.admin-panel {
  left: auto;
  right: 12px;
  width: min(324px, calc(100vw - 24px));
  max-height: calc(100dvh - 24px);
  overflow-y: auto;
  pointer-events: auto;
  scrollbar-gutter: stable;
}

.admin-gate {
  position: absolute;
  inset: 0;
  z-index: 3;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(8, 11, 12, 0.46);
  backdrop-filter: blur(8px);
}

.admin-login {
  width: min(360px, 100%);
  display: grid;
  gap: 10px;
  padding: 14px;
  border: 1px solid var(--hud-border);
  border-radius: 7px;
  background: var(--hud-bg-strong);
  box-shadow: 0 16px 42px rgba(0, 0, 0, 0.28);
}

.chat-overlay {
  position: absolute;
  bottom: 12px;
  left: 12px;
  z-index: 1;
  width: min(380px, calc(100vw - 24px));
  max-height: min(24vh, 190px);
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  gap: 4px;
  overflow: hidden;
  color: rgba(238, 244, 239, 0.9);
  pointer-events: none;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.66);
}

.chat-line {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: baseline;
  gap: 6px;
  width: fit-content;
  max-width: 100%;
  padding: 3px 7px;
  border-radius: 5px;
  background: linear-gradient(90deg, rgba(8, 11, 12, 0.38), rgba(8, 11, 12, 0.12));
  backdrop-filter: blur(4px);
  animation: chatLineIn 160ms ease-out;
}

.chat-user,
.chat-text {
  min-width: 0;
  font-size: 11px;
  line-height: 1.25;
}

.chat-user {
  max-width: 120px;
  overflow: hidden;
  color: var(--hud-accent-strong);
  font-weight: 700;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-text {
  display: -webkit-box;
  overflow: hidden;
  color: rgba(238, 244, 239, 0.88);
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
}

@keyframes chatLineIn {
  from {
    opacity: 0;
    transform: translateY(4px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 680px) {
  .viewer-stage.is-split {
    gap: 3px;
    padding: 3px;
    padding-bottom: calc(min(46dvh, 340px) + max(12px, env(safe-area-inset-bottom)));
  }

  body[data-route="admin"][data-admin-panel="collapsed"] .viewer-stage.is-split {
    padding-bottom: calc(112px + max(12px, env(safe-area-inset-bottom)));
  }

  body[data-route="admin"][data-admin-panel="expanded"] .viewer-stage.is-split {
    padding-bottom: calc(min(46dvh, 340px) + max(12px, env(safe-area-inset-bottom)));
  }

  .viewer-stage.is-split[data-layout="2"],
  .viewer-stage.is-split[data-layout="4"],
  .viewer-stage.is-split[data-layout="6"],
  .viewer-stage.is-split[data-layout="9"] {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .viewer-stage.is-split[data-layout="9"] {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .viewer-tile {
    border-radius: 4px;
  }

  .viewer-tile-label {
    top: 4px;
    left: 4px;
    max-width: calc(100% - 8px);
    padding: 3px 5px;
    font-size: 10px;
  }

  .panel {
    top: max(8px, env(safe-area-inset-top));
    left: 8px;
    width: calc(100vw - 16px);
    gap: 6px;
    padding: 7px;
    border-radius: 7px;
    background: rgba(13, 17, 18, 0.56);
    box-shadow: 0 8px 22px rgba(0, 0, 0, 0.2);
    backdrop-filter: blur(7px);
  }

  .brand {
    align-items: center;
    gap: 6px;
  }

  .brand > div:first-child {
    min-width: 0;
  }

  .brand p {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .brand-actions {
    gap: 4px;
  }

  h1 {
    font-size: 14px;
  }

  p {
    margin-top: 1px;
    font-size: 10px;
  }

  a,
  button,
  input,
  select {
    min-height: 32px;
    font-size: 13px;
  }

  button {
    padding: 0 8px;
  }

  .icon-button {
    width: 32px;
    font-size: 12px;
  }

  .field {
    gap: 3px;
    font-size: 10px;
  }

  #publicPanel.panel {
    width: min(392px, calc(100vw - 16px));
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px;
  }

  #publicPanel .brand {
    order: 2;
    flex: 0 0 auto;
    gap: 0;
  }

  #publicPanel .brand > div:first-child,
  #publicPanel .stats,
  #publicPanel > .field > span {
    display: none;
  }

  #publicPanel > .field {
    order: 1;
    flex: 1 1 auto;
    min-width: 0;
  }

  #publicPanel select {
    min-height: 32px;
    padding: 0 8px;
  }

  .admin-mobile-toggle {
    display: grid;
  }

  .admin-panel {
    top: auto;
    right: 8px;
    bottom: max(8px, env(safe-area-inset-bottom));
    left: 8px;
    width: auto;
    max-height: min(46dvh, 340px);
    overflow-y: auto;
    pointer-events: auto;
    scrollbar-gutter: stable;
  }

  .admin-panel::before {
    content: "";
    width: 34px;
    height: 3px;
    justify-self: center;
    border-radius: 999px;
    background: rgba(238, 244, 239, 0.34);
  }

  .admin-panel .brand {
    min-height: 32px;
  }

  .admin-panel .brand h1 {
    font-size: 14px;
  }

  .admin-panel .brand p {
    max-width: 38vw;
  }

  .admin-panel-controls {
    gap: 6px;
  }

  body[data-route="admin"][data-admin-panel="collapsed"] .admin-panel {
    max-height: 112px;
    overflow: hidden;
  }

  body[data-route="admin"][data-admin-panel="collapsed"] .admin-split-controls,
  body[data-route="admin"][data-admin-panel="collapsed"] .toolbar,
  body[data-route="admin"][data-admin-panel="collapsed"] .admin-chat {
    display: none;
  }

  body[data-route="admin"][data-admin-panel="collapsed"] .admin-controls .field > span {
    display: none;
  }

  .admin-split-controls {
    grid-template-columns: minmax(78px, 0.55fr) minmax(0, 1.45fr);
    gap: 6px;
  }

  .admin-controls {
    gap: 6px;
  }

  .camera-list {
    max-height: 58px;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 4px;
  }

  .camera-toggle {
    min-height: 28px;
    gap: 4px;
    padding: 0 6px;
  }

  .camera-toggle input {
    width: 12px;
    min-width: 12px;
    min-height: 12px;
  }

  .camera-toggle span {
    font-size: 10px;
  }

  .toolbar {
    gap: 5px;
  }

  .admin-panel .toolbar button {
    min-width: 0;
    padding: 0 6px;
    font-size: 12px;
  }

  .admin-chat summary {
    min-height: 32px;
  }

  .admin-chat[open] {
    gap: 6px;
  }

  .chat-form {
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: end;
    gap: 6px;
  }

  .chat-form .field {
    min-width: 0;
  }

  .chat-form button {
    padding: 0 10px;
  }

  .chat-overlay {
    bottom: max(8px, env(safe-area-inset-bottom));
    left: 8px;
    width: min(340px, calc(100vw - 16px));
    max-height: 16vh;
    gap: 2px;
  }

  .chat-line {
    max-width: 92%;
    padding: 2px 6px;
    background: linear-gradient(90deg, rgba(8, 11, 12, 0.3), rgba(8, 11, 12, 0.08));
  }

  .chat-user,
  .chat-text {
    font-size: 10px;
  }

  body[data-route="admin"][data-admin-panel="collapsed"] .chat-overlay {
    bottom: calc(112px + max(12px, env(safe-area-inset-bottom)));
    max-height: 13vh;
  }

  body[data-route="admin"][data-admin-panel="expanded"] .chat-overlay {
    bottom: calc(min(46dvh, 340px) + max(12px, env(safe-area-inset-bottom)));
    max-height: 13vh;
  }
}

@media (max-width: 390px) {
  body[data-route="admin"][data-admin-panel="expanded"] .viewer-stage.is-split {
    padding-bottom: calc(min(52dvh, 360px) + max(12px, env(safe-area-inset-bottom)));
  }

  .admin-panel {
    max-height: min(52dvh, 360px);
  }

  body[data-route="admin"][data-admin-panel="collapsed"] .admin-panel {
    max-height: 106px;
  }

  .admin-panel .brand p {
    max-width: 30vw;
  }

  .admin-split-controls {
    grid-template-columns: 1fr;
  }

  .camera-list {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .chat-form {
    grid-template-columns: 1fr;
  }

  body[data-route="admin"][data-admin-panel="collapsed"] .chat-overlay {
    bottom: calc(106px + max(12px, env(safe-area-inset-bottom)));
  }

  body[data-route="admin"][data-admin-panel="expanded"] .chat-overlay {
    bottom: calc(min(52dvh, 360px) + max(12px, env(safe-area-inset-bottom)));
  }
}

body[data-hud="hidden"] .panel {
  display: none;
}

body[data-chat="hidden"] .chat-overlay {
  display: none;
}

body[data-hud="compact"] .panel {
  width: min(260px, calc(100vw - 20px));
}

body[data-hud="compact"] .stats {
  display: none;
}
