:root {
  color-scheme: dark;
  --fg: #f2f2f2;
  --dim: #9a9a9a;
  --line: rgba(255, 255, 255, 0.18);
  --accent: #ffd24a;
  --bad: #ff6b5a;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: #000; color: var(--fg); }
body {
  font: 14px/1.35 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: grid;
  grid-template-rows: auto 1fr auto;
  height: 100dvh;
  overflow: hidden;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}
button, select, input { font: inherit; color: inherit; }
button {
  background: none;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 6px 12px;
  cursor: pointer;
}
button:active { background: rgba(255, 255, 255, 0.12); }
button.on { color: #000; background: var(--accent); border-color: var(--accent); }
select { background: #111; border: 1px solid var(--line); border-radius: 6px; padding: 4px 6px; max-width: 60vw; }
a { color: var(--fg); }

#top, #bottom {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  padding-left: max(12px, env(safe-area-inset-left));
  padding-right: max(12px, env(safe-area-inset-right));
}
#top { padding-top: max(8px, env(safe-area-inset-top)); }
#top .status { margin-left: auto; border: none; color: var(--dim); }
#top .status.linked { color: var(--accent); }
#top .status.bad { color: var(--bad); }
#bottom {
  justify-content: space-between;
  padding-bottom: max(14px, env(safe-area-inset-bottom));
  padding-top: 10px;
}
#bottom > button:not(#b-shutter) { min-width: 84px; }
#b-shutter {
  width: 68px; height: 68px;
  border-radius: 50%;
  border: 4px solid #fff;
  background: rgba(255, 255, 255, 0.15);
  padding: 0;
}
#b-shutter:active, #b-shutter.busy { background: #fff; }
#count { color: var(--dim); margin-left: 4px; }

#viewport {
  position: relative;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}
#stage { position: relative; background: #050505; touch-action: none; }
#stage video, #stage canvas {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  display: block;
}
#stage video { object-fit: cover; }
#overlay { pointer-events: none; }
#overlay.opaque { background: #000; }
#flash { position: absolute; inset: 0; background: #fff; opacity: 0; pointer-events: none; }
#flash.go { animation: flash 0.25s ease-out; }
@keyframes flash { from { opacity: 0.7; } to { opacity: 0; } }

.panel {
  position: absolute;
  max-width: min(420px, calc(100% - 32px));
  background: rgba(0, 0, 0, 0.82);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 16px;
  text-align: center;
}
#scanbox { bottom: 16px; }
#qr { background: #fff; padding: 12px; border-radius: 8px; display: inline-block; line-height: 0; }
#qr svg, #qr img { width: min(280px, 60vw); height: auto; }
.dim { color: var(--dim); }
.small { font-size: 12px; }
#pairurl { word-break: break-all; user-select: text; }

#toast {
  position: fixed;
  left: 50%; bottom: calc(120px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: rgba(30, 30, 30, 0.95);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 14px;
  max-width: calc(100% - 32px);
}

#settings {
  width: min(560px, 100%);
  max-width: 100%;
  max-height: 85dvh;
  margin: auto auto 0;
  background: #111;
  color: var(--fg);
  border: none;
  border-top: 1px solid var(--line);
  border-radius: 14px 14px 0 0;
  padding: 12px 16px max(16px, env(safe-area-inset-bottom));
}
#settings::backdrop { background: rgba(0, 0, 0, 0.6); }
.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 44px;
}
.row label { color: var(--dim); flex: none; }
.row input[type=range] { flex: 1; max-width: 240px; }
.stepper { display: inline-flex; align-items: center; gap: 10px; }
.stepper output { min-width: 2ch; text-align: center; }
hr { border: none; border-top: 1px solid var(--line); margin: 8px 0; }
button.wide { width: 100%; margin-top: 8px; }

body.role-remote [data-only=camera],
body.role-camera [data-only=remote] { display: none !important; }
[hidden] { display: none !important; }

#controls {
  position: absolute;
  left: 50%;
  bottom: 8px;
  transform: translateX(-50%);
  width: min(560px, calc(100% - 16px));
  background: rgba(0, 0, 0, 0.78);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 6px 12px;
  z-index: 1;
}
.control {
  display: grid;
  grid-template-columns: 74px 56px 1fr 64px;
  align-items: center;
  gap: 8px;
  min-height: 40px;
}
.control > span { color: var(--dim); }
.control button { padding: 3px 8px; font-size: 12px; }
.control input[type=range] { width: 100%; }
.control output { text-align: right; font-variant-numeric: tabular-nums; }
.control.auto input[type=range], .control.auto output { opacity: 0.45; }

@media (orientation: landscape) and (max-height: 600px) {
  body {
    grid-template-rows: 100dvh;
    grid-template-columns: auto 1fr auto;
  }
  #top, #bottom {
    flex-direction: column;
    justify-content: space-between;
    gap: 10px;
    padding: max(10px, env(safe-area-inset-top)) 10px max(10px, env(safe-area-inset-bottom));
  }
  #top { padding-left: max(10px, env(safe-area-inset-left)); }
  #bottom { padding-right: max(10px, env(safe-area-inset-right)); }
  #top > button { min-width: 64px; }
  #top .status {
    order: 1;
    margin: auto 0;
    max-width: 72px;
    font-size: 12px;
    text-align: center;
    white-space: normal;
  }
  #top #b-menu { order: 2; }
  #bottom > button:not(#b-shutter) { min-width: 64px; }
  #settings {
    margin: 0 0 0 auto;
    width: min(420px, 75vw);
    height: 100dvh;
    max-height: 100dvh;
    border-top: none;
    border-left: 1px solid var(--line);
    border-radius: 14px 0 0 14px;
    padding: max(12px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) 12px 16px;
  }
  #toast { bottom: 16px; }
  #controls { bottom: 6px; padding: 2px 10px; }
  .control { min-height: 34px; }
}
