:root {
  color-scheme: light dark;
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #eef0f4;
  --border: #e1e4ea;
  --text: #111318;
  --muted: #5d6471;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --on-accent: #ffffff;
  --accent-soft: rgb(37 99 235 / 0.1);
  --danger: #dc2626;
  --live: #ef4444;
  --ok: #16a34a;
  --warn: #b45309;
  --warn-soft: rgb(217 119 6 / 0.12);
  --stage: #07080b;
  --radius: 16px;
  --shadow: 0 1px 2px rgb(16 24 40 / 0.05), 0 10px 30px -12px rgb(16 24 40 / 0.18);
  --topbar: 60px;
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b0d11;
    --surface: #14171d;
    --surface-2: #1c2029;
    --border: #272c36;
    --text: #e8eaef;
    --muted: #9aa1ae;
    --accent: #5b8def;
    --accent-hover: #7aa3f3;
    --on-accent: #0b0d11;
    --accent-soft: rgb(91 141 239 / 0.14);
    --danger: #ef4444;
    --live: #f05252;
    --ok: #34c77b;
    --warn: #f5a524;
    --warn-soft: rgb(245 165 36 / 0.12);
    --shadow: 0 1px 2px rgb(0 0 0 / 0.3), 0 10px 30px -12px rgb(0 0 0 / 0.6);
  }
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
}

a {
  color: var(--accent);
}

h1,
h2,
h3 {
  margin: 0;
  line-height: 1.2;
  letter-spacing: -0.01em;
}

p {
  margin: 0;
}

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

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.sprite {
  display: none;
}

.icon {
  width: 20px;
  height: 20px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

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

.small {
  font-size: 0.875rem;
}

/* Top bar */

.topbar {
  height: var(--topbar);
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 max(16px, env(safe-area-inset-right)) 0 max(16px, env(safe-area-inset-left));
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: var(--text);
  font-weight: 700;
  font-size: 1.05rem;
  text-decoration: none;
  letter-spacing: -0.01em;
}

.brand-mark {
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  border-radius: 9px;
  color: #fff;
  background: linear-gradient(135deg, #3b82f6, #8b5cf6);
}

.brand-mark .icon {
  width: 20px;
  height: 20px;
}

.icon-link {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  color: var(--muted);
}

.icon-link:hover {
  color: var(--text);
  background: var(--surface-2);
}

main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.screen {
  flex: 1;
  min-width: 0;
}

/* Buttons and fields */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  padding: 0 18px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  transition: background-color 0.15s, border-color 0.15s, transform 0.05s;
}

.btn:hover {
  background: var(--surface-2);
}

.btn:active {
  transform: translateY(1px);
}

.btn:disabled {
  opacity: 0.55;
  cursor: default;
}

.btn.primary {
  background: var(--accent);
  border-color: transparent;
  color: var(--on-accent);
}

.btn.primary:hover:not(:disabled) {
  background: var(--accent-hover);
}

.btn.ghost {
  background: transparent;
}

.btn.small {
  min-height: 36px;
  padding: 0 12px;
  border-radius: 10px;
  font-size: 0.875rem;
}

.btn.small .icon {
  width: 16px;
  height: 16px;
}

.fields {
  display: grid;
  gap: 12px;
}

.field {
  display: grid;
  gap: 6px;
  font-size: 0.875rem;
  font-weight: 600;
}

.field > span {
  color: var(--muted);
}

.field select,
.field input,
.link-row input {
  width: 100%;
  min-height: 44px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  font-weight: 500;
}

.field select:disabled {
  opacity: 0.7;
}

.check {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 32px;
  font-size: 0.95rem;
  cursor: pointer;
}

.check input {
  width: 18px;
  height: 18px;
  margin: 0;
  accent-color: var(--accent);
}

.code-input {
  font-family: var(--mono);
  font-size: 1.25rem !important;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.code-input::placeholder {
  letter-spacing: 0.12em;
  color: var(--muted);
  opacity: 0.5;
}

.form-error {
  color: var(--danger);
  font-size: 0.9rem;
}

/* Home */

.home {
  width: 100%;
  max-width: 980px;
  margin: 0 auto;
  padding: 24px 16px 48px;
  display: grid;
  gap: 32px;
  align-content: start;
}

.hero {
  text-align: center;
  padding-top: 16px;
}

.hero h1 {
  font-size: clamp(1.9rem, 5vw, 3rem);
  font-weight: 750;
  letter-spacing: -0.03em;
}

.lede {
  max-width: 620px;
  margin: 14px auto 0;
  color: var(--muted);
  font-size: 1.1rem;
}

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 300px), 1fr));
  gap: 16px;
}

.card {
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-width: 0;
  padding: 24px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.card h2 {
  font-size: 1.3rem;
}

.card > .btn {
  margin-top: auto;
}

.card-icon {
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border-radius: 12px;
  background: var(--accent-soft);
  color: var(--accent);
}

.card-icon .icon {
  width: 24px;
  height: 24px;
}

.steps {
  list-style: none;
  counter-reset: step;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 220px), 1fr));
  gap: 16px;
}

.steps li {
  counter-increment: step;
  display: flex;
  gap: 12px;
  align-items: baseline;
  color: var(--muted);
}

.steps li::before {
  content: counter(step);
  flex: none;
  width: 28px;
  height: 28px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: var(--surface-2);
  color: var(--text);
  font-weight: 700;
  font-size: 0.875rem;
  transform: translateY(-2px);
}

.steps strong {
  color: var(--text);
}

.fineprint {
  max-width: 720px;
  margin: 0 auto;
  text-align: center;
  color: var(--muted);
  font-size: 0.85rem;
}

/* Video stage */

.stage {
  position: relative;
  overflow: hidden;
  background: var(--stage);
  color: #fff;
  border-radius: var(--radius);
  isolation: isolate;
}

.stage video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: var(--stage);
}

.stage.mirror video {
  transform: scaleX(-1);
}

.stage.fill video {
  object-fit: cover;
}

.overlay-top {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 2;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
  padding: 12px;
  pointer-events: none;
}

.overlay-top > *,
.overlay-right > * {
  pointer-events: auto;
}

.overlay-right {
  display: flex;
  align-items: center;
  gap: 8px;
  pointer-events: none;
}

.pill {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  height: 32px;
  padding: 0 12px;
  border-radius: 999px;
  background: rgb(0 0 0 / 0.55);
  color: #fff;
  font-size: 0.85rem;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.pill .icon {
  width: 16px;
  height: 16px;
}

.pill .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #a1a1aa;
}

.pill[data-state="live"] .dot {
  background: var(--live);
  animation: pulse 1.6s ease-in-out infinite;
}

.pill[data-state="connecting"] .dot,
.pill[data-state="reconnecting"] .dot {
  background: #fbbf24;
}

.pill[data-state="error"] .dot,
.pill[data-state="id-taken"] .dot {
  background: var(--danger);
}

.pill.rec .dot {
  background: var(--live);
  animation: pulse 1s ease-in-out infinite;
}

@keyframes pulse {
  50% {
    opacity: 0.35;
  }
}

.controls {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 2;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
  padding: 28px 12px max(14px, env(safe-area-inset-bottom));
  background: linear-gradient(transparent, rgb(0 0 0 / 0.55));
  transition: opacity 0.25s;
}

.ctl {
  position: relative;
  width: 48px;
  height: 48px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: rgb(255 255 255 / 0.16);
  color: #fff;
  cursor: pointer;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  transition: background-color 0.15s, color 0.15s;
}

.ctl:hover:not(:disabled) {
  background: rgb(255 255 255 / 0.28);
}

.ctl:disabled {
  opacity: 0.45;
  cursor: default;
}

.ctl .icon {
  width: 22px;
  height: 22px;
}

.ctl.small {
  width: 36px;
  height: 36px;
  background: rgb(0 0 0 / 0.55);
}

.ctl.small .icon {
  width: 18px;
  height: 18px;
}

.ctl[aria-pressed="true"] {
  background: #fff;
  color: #111318;
}

.ctl.danger {
  background: var(--danger);
  color: #fff;
}

.ctl.danger:hover:not(:disabled) {
  background: #b91c1c;
}

#record[aria-pressed="true"] {
  background: var(--live);
  color: #fff;
}

.ctl .when-on {
  display: none;
}

.ctl[aria-pressed="true"] .when-on {
  display: block;
}

.ctl[aria-pressed="true"] .when-off {
  display: none;
}

.stage-note,
.waiting {
  position: absolute;
  inset: 0;
  z-index: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 72px 24px 110px;
  text-align: center;
  background: rgb(7 8 11 / 0.72);
}

.waiting h2 {
  font-size: 1.35rem;
}

.waiting p {
  max-width: 420px;
  color: rgb(255 255 255 / 0.75);
}

.waiting .code {
  margin-top: 4px;
  color: #fff;
  font-size: 1rem;
  opacity: 0.8;
}

.waiting .btn {
  margin-top: 6px;
}

.spinner {
  width: 36px;
  height: 36px;
  margin-bottom: 6px;
  border: 3px solid rgb(255 255 255 / 0.2);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}

.waiting[data-state="error"] .spinner {
  display: none;
}

.waiting[data-state="offline"] .spinner,
.waiting[data-state="ended"] .spinner {
  animation-duration: 2.4s;
  border-top-color: rgb(255 255 255 / 0.6);
}

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

.play-button {
  position: absolute;
  z-index: 3;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.stats {
  position: absolute;
  z-index: 2;
  top: 56px;
  left: 12px;
  margin: 0;
  padding: 10px 12px;
  border-radius: 10px;
  background: rgb(0 0 0 / 0.62);
  color: #fff;
  font: 12px/1.6 var(--mono);
  pointer-events: none;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

/* Idle viewer: controls fade out, and a tap brings them back rather than pressing a button blind. */
.stage.idle .controls,
.stage.idle .overlay-top {
  opacity: 0;
  pointer-events: none;
}

.stage.idle .overlay-top > *,
.stage.idle .overlay-right > * {
  pointer-events: none;
}

.stage.idle {
  cursor: none;
}

.overlay-top {
  transition: opacity 0.25s;
}

/* Broadcast */

.broadcast {
  width: 100%;
  max-width: 1320px;
  margin: 0 auto;
  padding: 8px 16px 32px;
  display: grid;
  gap: 16px;
  align-content: start;
}

#broadcast-stage {
  --ratio: 1.7778;
  --max-h: calc(100dvh - var(--topbar) - 48px);
  width: min(100%, calc(var(--max-h) * var(--ratio)));
  aspect-ratio: var(--ratio);
  max-height: var(--max-h);
  min-height: 240px;
  justify-self: center;
}

.panel {
  display: grid;
  gap: 14px;
  align-content: start;
  min-width: 0;
  padding: 20px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.panel h2 {
  font-size: 1.05rem;
}

.panel h2:not(:first-of-type) {
  margin-top: 8px;
}

.share {
  display: flex;
  gap: 16px;
  align-items: center;
}

.qr {
  flex: none;
  width: 132px;
  height: 132px;
  border-radius: 10px;
  overflow: hidden;
  background: #fff;
  box-shadow: 0 0 0 1px var(--border);
}

.qr svg {
  display: block;
  width: 100%;
  height: 100%;
}

.share-info {
  display: grid;
  gap: 4px;
  min-width: 0;
}

.label {
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.code {
  font-family: var(--mono);
  font-size: 1.6rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  line-height: 1.2;
  white-space: nowrap;
}

.share .code {
  font-size: 1.35rem;
}

.link-row {
  display: flex;
  gap: 8px;
}

.link-row input {
  flex: 1;
  min-width: 0;
  min-height: 36px;
  font-size: 0.85rem;
  color: var(--muted);
}

.button-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.alert {
  display: grid;
  gap: 10px;
  justify-items: start;
  padding: 12px 14px;
  border-radius: 12px;
  background: var(--warn-soft);
  color: var(--text);
  font-size: 0.9rem;
}

.alert.error {
  background: rgb(220 38 38 / 0.12);
}

@media (min-width: 960px) {
  .broadcast {
    grid-template-columns: minmax(0, 1fr) 340px;
    align-items: start;
  }

  #broadcast-stage {
    --max-h: calc(100dvh - var(--topbar) - 40px);
  }
}

/* Watch: the stage takes the whole window below the top bar. */

body[data-screen="watch"] .topbar {
  display: none;
}

.watch {
  display: flex;
}

#watch-stage {
  flex: 1;
  min-height: 100dvh;
  border-radius: 0;
}

#watch-stage:fullscreen {
  width: 100%;
  height: 100%;
}

/* Toast */

.toast {
  position: fixed;
  z-index: 10;
  left: 50%;
  bottom: max(24px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  padding: 10px 16px;
  border-radius: 12px;
  background: var(--text);
  color: var(--bg);
  font-size: 0.9rem;
  font-weight: 550;
  box-shadow: var(--shadow);
}

@media (max-width: 520px) {
  .home {
    gap: 24px;
    padding-top: 8px;
  }

  .card {
    padding: 20px;
  }

  .broadcast {
    padding: 0 16px 24px;
    gap: 12px;
  }

  #broadcast-stage {
    --max-h: 62dvh;
  }

  .panel {
    padding: 16px;
  }

  .share {
    align-items: flex-start;
  }

  .qr {
    width: 112px;
    height: 112px;
  }

  .code {
    font-size: 1.35rem;
  }

  /* Seven viewer controls in one row on a 360px-wide phone. */
  .controls {
    gap: 6px;
    padding-left: 8px;
    padding-right: 8px;
  }

  .ctl {
    width: 42px;
    height: 42px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition: none !important;
  }
}
