:root {
  --bg: #070b10;
  --bg2: #0b121a;
  --panel: #0f1822;
  --line: #1c2a38;
  --text: #e9f2fa;
  --muted: #7f95a8;
  --accent: #34e0a1;
  --accent-dim: #1a8f68;
  --cyan: #4fd1ff;
  --warn: #ffbe55;
  --danger: #ff7373;
  --radius: 14px;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

[hidden] {
  display: none !important;
}

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "PingFang SC", "HarmonyOS Sans SC", "Microsoft YaHei", "Segoe UI", system-ui, -apple-system, sans-serif;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
}

body {
  overflow: hidden;
}

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

button {
  border: 1px solid var(--line);
  background: #121c26;
  color: var(--text);
  border-radius: 11px;
  padding: 10px 14px;
  cursor: pointer;
  transition: transform .08s ease, background .15s ease, border-color .15s ease;
}

button:active {
  transform: scale(.97);
}

button:disabled {
  opacity: .45;
  pointer-events: none;
}

button.primary {
  background: linear-gradient(180deg, #40f0ae, #24c98c);
  border-color: #1f9f74;
  color: #04130d;
  font-weight: 700;
}

button.primary.stop {
  background: linear-gradient(180deg, #ff9a8e, #e0615a);
  border-color: #b94842;
  color: #1b0705;
}

button.primary.big {
  width: 100%;
  padding: 15px 18px;
  font-size: 17px;
  margin: 12px 0 8px;
}

button.ghost {
  background: rgba(255, 255, 255, .035);
}

button.danger {
  border-color: rgba(255, 115, 115, .4);
  color: var(--danger);
}

#app {
  height: 100vh;
  height: 100dvh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
}

/* ---------- HUD ---------- */
#hud {
  padding: calc(env(safe-area-inset-top) + 8px) 12px 9px;
  background: linear-gradient(180deg, #0c141d 0%, #0a1118 100%);
  border-bottom: 1px solid var(--line);
  z-index: 5;
}

.hudTop {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 7px;
  min-width: 0;
}

.brandName {
  font-size: 15px;
  font-weight: 650;
  letter-spacing: .04em;
  white-space: nowrap;
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #3a4b5a;
  flex: none;
}

.dot.live {
  background: var(--danger);
  box-shadow: 0 0 0 0 rgba(255, 115, 115, .5);
  animation: pulse 1.6s infinite;
}

@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(255, 115, 115, .55); }
  70% { box-shadow: 0 0 0 9px rgba(255, 115, 115, 0); }
  100% { box-shadow: 0 0 0 0 rgba(255, 115, 115, 0); }
}

.tag {
  font-size: 10px;
  color: var(--muted);
  background: #101a23;
  border: 1px solid var(--line);
  padding: 2px 7px;
  border-radius: 99px;
  white-space: nowrap;
}

.tag.rec {
  color: var(--danger);
  border-color: rgba(255, 115, 115, .38);
}

.pills {
  display: flex;
  gap: 4px;
  flex-wrap: nowrap;
  overflow: hidden;
}

.pill {
  font-size: 10px;
  padding: 3px 7px;
  border-radius: 99px;
  border: 1px solid var(--line);
  color: var(--muted);
  background: #0d161f;
  white-space: nowrap;
}

.pill.on {
  color: #06130d;
  background: var(--accent);
  border-color: var(--accent);
}

.pill.warn {
  color: var(--warn);
  border-color: rgba(255, 190, 85, .4);
}

.pill.err {
  color: var(--danger);
  border-color: rgba(255, 115, 115, .4);
}

.stats {
  display: flex;
  align-items: flex-end;
  gap: 16px;
  margin-top: 9px;
}

.stat {
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
}

.stat b {
  font-size: 21px;
  line-height: 1.05;
  font-variant-numeric: tabular-nums;
  letter-spacing: -.01em;
}

.stat span {
  font-size: 10px;
  color: var(--muted);
  white-space: nowrap;
}

.stat.main b {
  font-size: 40px;
  color: var(--accent);
  line-height: .95;
  text-shadow: 0 0 22px rgba(52, 224, 161, .25);
}

.bar {
  display: flex;
  align-items: center;
  gap: 9px;
  margin-top: 9px;
}

.barTrack {
  flex: 1;
  height: 4px;
  border-radius: 99px;
  background: #14202b;
  overflow: hidden;
}

#barFill {
  height: 100%;
  width: 0%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--accent-dim), var(--accent));
  transition: width .18s linear;
}

#nextIn {
  font-size: 11px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  min-width: 34px;
  text-align: right;
}

/* ---------- Stage ---------- */
#stage {
  position: relative;
  min-height: 0;
  background: #000;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

#frame {
  position: relative;
  width: 1px;
  height: 1px;
  background: #000;
  box-shadow: 0 0 40px rgba(0, 0, 0, .6);
}

#video {
  position: absolute;
  left: 0;
  top: 0;
  width: 2px;
  height: 2px;
  opacity: 0.001;
  pointer-events: none;
}

#view, #overlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

#overlay {
  touch-action: none;
  pointer-events: none;
}

#stage.editing #overlay {
  pointer-events: auto;
  cursor: crosshair;
}

#stage.editing #frame {
  outline: 1px solid rgba(79, 209, 255, .35);
}

.tools {
  position: absolute;
  top: 10px;
  right: 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  z-index: 4;
}

button.tool {
  font-size: 11px;
  padding: 8px 10px;
  border-radius: 10px;
  background: rgba(8, 15, 22, .62);
  backdrop-filter: blur(8px);
  border-color: rgba(255, 255, 255, .1);
  color: #cfe2f1;
}

#stage.editing button.tool.active {
  background: rgba(79, 209, 255, .18);
  border-color: rgba(79, 209, 255, .55);
  color: var(--cyan);
}

.editbar {
  position: absolute;
  left: 10px;
  right: 10px;
  bottom: 10px;
  z-index: 5;
  background: rgba(8, 15, 22, .82);
  border: 1px solid rgba(79, 209, 255, .3);
  border-radius: 12px;
  padding: 9px 10px;
  backdrop-filter: blur(10px);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.editLabel {
  font-size: 11px;
  color: #b9d6ea;
  line-height: 1.35;
}

.editActions {
  display: flex;
  gap: 6px;
}

.editActions button {
  flex: 1;
  padding: 8px 6px;
  font-size: 12px;
}

.editActions button.ok {
  background: var(--cyan);
  color: #04151d;
  border-color: var(--cyan);
  font-weight: 700;
}

.camPrompt {
  position: absolute;
  inset: 0;
  z-index: 8;
  display: grid;
  place-items: center;
  padding: 20px;
  background:
    radial-gradient(1200px 500px at 50% -10%, rgba(52, 224, 161, .12), transparent 60%),
    linear-gradient(180deg, #05080c, #070d13);
}

.promptCard {
  width: min(360px, 100%);
  text-align: center;
  background: rgba(15, 24, 34, .9);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 22px 20px 16px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, .5);
}

.promptIcon {
  font-size: 34px;
  color: var(--accent);
  line-height: 1;
  margin-bottom: 6px;
}

.promptCard h2 {
  font-size: 19px;
  margin: 6px 0 8px;
}

.promptCard p {
  font-size: 12.5px;
  color: var(--muted);
  line-height: 1.55;
  margin: 0 0 8px;
}

.tiny {
  font-size: 11px;
}

.hint {
  position: absolute;
  top: 12px;
  left: 10px;
  right: 58px;
  z-index: 4;
  font-size: 11px;
  line-height: 1.45;
  color: #cfe6f5;
  background: rgba(8, 15, 22, .7);
  border: 1px solid rgba(255, 255, 255, .08);
  border-radius: 10px;
  padding: 7px 9px;
  backdrop-filter: blur(8px);
  pointer-events: none;
}

.toast {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(.96);
  z-index: 9;
  background: rgba(10, 18, 26, .94);
  border: 1px solid rgba(52, 224, 161, .45);
  color: var(--text);
  padding: 10px 16px;
  border-radius: 12px;
  font-size: 13px;
  max-width: 86%;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity .18s ease, transform .18s ease;
}

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

.toast.err {
  border-color: rgba(255, 115, 115, .6);
}

.loading {
  position: absolute;
  inset: 0;
  z-index: 7;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  background: rgba(4, 8, 12, .55);
  backdrop-filter: blur(2px);
  font-size: 13px;
  color: #d7e8f5;
}

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

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

/* ---------- Controls ---------- */
#controls {
  display: flex;
  gap: 8px;
  padding: 9px 10px calc(env(safe-area-inset-bottom) + 9px);
  background: linear-gradient(180deg, #0a1118, #0c141d);
  border-top: 1px solid var(--line);
  z-index: 5;
}

#controls .primary {
  flex: 1;
  padding: 13px 16px;
  font-size: 16px;
}

#controls .compact {
  flex: none;
  min-width: 58px;
  padding: 13px 10px;
  font-size: 14px;
}

#btnManual {
  color: var(--accent);
  border-color: rgba(52, 224, 161, .4);
  font-weight: 700;
  font-size: 16px;
}

/* ---------- Dialog ---------- */
dialog {
  margin: auto;
  padding: 0;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  border-radius: 18px;
  width: min(560px, 94vw);
  max-height: 88vh;
  max-height: 88dvh;
  overflow: hidden;
  box-shadow: 0 30px 80px rgba(0, 0, 0, .6);
}

dialog::backdrop {
  background: rgba(2, 5, 8, .68);
  backdrop-filter: blur(4px);
}

.dlgHead {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px 10px;
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  background: var(--panel);
  z-index: 2;
}

.dlgHead h2 {
  margin: 0;
  font-size: 16px;
}

.dlgHead .x {
  border: 0;
  background: transparent;
  font-size: 24px;
  line-height: 1;
  padding: 2px 6px;
  color: var(--muted);
}

.dlgBody {
  padding: 4px 16px 18px;
  overflow-y: auto;
  max-height: calc(88dvh - 58px);
}

.dlgBody section {
  padding: 12px 0 4px;
  border-bottom: 1px solid rgba(28, 42, 56, .7);
}

.dlgBody section:last-of-type {
  border-bottom: 0;
}

.dlgBody h3 {
  margin: 0 0 4px;
  font-size: 12px;
  letter-spacing: .12em;
  color: var(--accent);
  font-weight: 650;
}

label.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 0;
  font-size: 13.5px;
}

label.row + label.row {
  border-top: 1px solid rgba(28, 42, 56, .5);
}

label.row.col {
  flex-direction: column;
  align-items: stretch;
  gap: 6px;
}

label.row > span {
  color: #d6e5f1;
}

label.row input[type="range"] {
  width: 44%;
  accent-color: var(--accent);
}

label.row output {
  min-width: 30px;
  text-align: right;
  font-variant-numeric: tabular-nums;
  color: var(--accent);
  font-size: 12px;
}

label.row input[type="checkbox"] {
  width: 20px;
  height: 20px;
  accent-color: var(--accent);
}

select, input[type="text"], input[type="url"] {
  background: #0a131b;
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 8px 10px;
  font-size: 13px;
  min-width: 0;
  max-width: 58%;
}

label.row.col input {
  max-width: none;
  width: 100%;
}

select:focus, input:focus {
  outline: none;
  border-color: rgba(52, 224, 161, .55);
  box-shadow: 0 0 0 3px rgba(52, 224, 161, .1);
}

.row.actions {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 0;
  flex-wrap: wrap;
}

.row.actions button {
  padding: 8px 12px;
  font-size: 12.5px;
}

.muted {
  color: var(--muted);
}

.rotateWarn {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 82px);
  transform: translateX(-50%);
  background: rgba(255, 190, 85, .14);
  border: 1px solid rgba(255, 190, 85, .35);
  color: var(--warn);
  font-size: 11px;
  padding: 5px 10px;
  border-radius: 99px;
  z-index: 6;
}

@media (min-width: 820px) {
  #hud { padding-left: 20px; padding-right: 20px; }
  #controls { padding-left: 20px; padding-right: 20px; }
  #controls .primary { flex: none; min-width: 220px; }
}

@media (max-width: 420px) {
  .stat.main b { font-size: 34px; }
  .stats { gap: 12px; }
  .stat b { font-size: 18px; }
  .pills { gap: 3px; }
  .pill { padding: 2px 5px; }
}

@media (max-width: 640px) {
  dialog {
    width: 100vw;
    max-width: 100vw;
    height: 100dvh;
    max-height: 100dvh;
    border-radius: 0;
    border: 0;
  }
  .dlgBody {
    max-height: calc(100dvh - 58px);
    padding-bottom: calc(env(safe-area-inset-bottom) + 20px);
  }
}
