:root {
  --bg: #0b0d10;
  --panel: #14181d;
  --panel-2: #1b2027;
  --line: #262d36;
  --fg: #e8edf3;
  --muted: #8b97a6;
  --accent: #4fc3f7;
  --live: #ff4757;
  --ok: #35d07f;
  --warn: #ffb020;
  --radius: 10px;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

html {
  /* Stops iOS Safari from bumping font sizes on rotation. */
  -webkit-text-size-adjust: 100%;
  /* Belt and braces: nothing in this layout may scroll sideways on a phone. */
  overflow-x: hidden;
}

header {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 20px;
  padding-top: calc(14px + env(safe-area-inset-top));
  padding-left: calc(20px + env(safe-area-inset-left));
  padding-right: calc(20px + env(safe-area-inset-right));
  border-bottom: 1px solid var(--line);
  background: var(--panel);
  flex-wrap: wrap;
}

header h1 {
  margin: 0;
  font-size: 17px;
  font-weight: 600;
  letter-spacing: -0.01em;
}

header .spacer { flex: 1; }

header a {
  color: var(--muted);
  text-decoration: none;
  font-size: 14px;
}
header a:hover { color: var(--fg); }

main {
  max-width: 1100px;
  margin: 0 auto;
  padding: 22px 20px 60px;
  padding-left: calc(20px + env(safe-area-inset-left));
  padding-right: calc(20px + env(safe-area-inset-right));
  padding-bottom: calc(60px + env(safe-area-inset-bottom));
}

/* ---- live badge ---- */

.badge {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  background: var(--panel-2);
  color: var(--muted);
  border: 1px solid var(--line);
}

.badge .dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: currentColor;
}

.badge.is-live {
  background: rgba(255, 71, 87, 0.12);
  border-color: rgba(255, 71, 87, 0.45);
  color: var(--live);
}

.badge.is-live .dot { animation: pulse 1.6s ease-in-out infinite; }

/* Standby: the card is playing, so this is not an error state — amber, not red. */
.badge.is-standby {
  background: rgba(255, 176, 66, 0.12);
  border-color: rgba(255, 176, 66, 0.45);
  color: #ffb042;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.25; }
}

/* ---- video ---- */

.stage {
  position: relative;
  background: #000;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  aspect-ratio: 16 / 9;
}

.stage video {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: contain;
  background: #000;
}

.stage .overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  gap: 12px;
  justify-items: center;
  text-align: center;
  padding: 24px;
  background: rgba(11, 13, 16, 0.92);
  color: var(--muted);
}

.stage .overlay[hidden] { display: none; }

.spinner {
  width: 26px;
  height: 26px;
  border: 2px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

/* ---- panels / controls ---- */

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px 18px;
  margin-top: 16px;
}

.panel h2 {
  margin: 0 0 12px;
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.row {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: flex-end;
}

.field { display: flex; flex-direction: column; gap: 5px; min-width: 150px; flex: 1; }

label {
  font-size: 12px;
  color: var(--muted);
  font-weight: 500;
}

select, input, button {
  font: inherit;
  color: var(--fg);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 8px 11px;
  /* iOS Safari applies its own chrome to form controls otherwise. */
  -webkit-appearance: none;
  appearance: none;
  /* 16px prevents iOS from zooming the page when a field takes focus. */
  font-size: 16px;
  /* Kills the grey flash on tap. */
  -webkit-tap-highlight-color: transparent;
}

/* Comfortable touch targets; 44px is Apple's guideline minimum. */
button, select { min-height: 44px; }

.chip {
  min-height: 34px;
  padding: 5px 11px;
  font-size: 13px;
}

select:focus, input:focus, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

button {
  cursor: pointer;
  font-weight: 600;
  white-space: nowrap;
  transition: background 0.15s, border-color 0.15s;
}

button:hover:not(:disabled) { border-color: #3a444f; background: #232a33; }
button:disabled { opacity: 0.45; cursor: not-allowed; }

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #06212c;
}
button.primary:hover:not(:disabled) { background: #6fd0f9; border-color: #6fd0f9; }

button.danger {
  background: rgba(255, 71, 87, 0.14);
  border-color: rgba(255, 71, 87, 0.5);
  color: #ff7b86;
}
button.danger:hover:not(:disabled) { background: rgba(255, 71, 87, 0.24); }

/* ---- stats ---- */

.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(108px, 1fr));
  gap: 10px;
}

.stat {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
}

.stat .k {
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.stat .v {
  font-family: var(--mono);
  font-size: 19px;
  font-variant-numeric: tabular-nums;
  margin-top: 3px;
}

.stat .v.good { color: var(--ok); }
.stat .v.warn { color: var(--warn); }
.stat .v.bad { color: var(--live); }

/* ---- viewer chat ---- */

.chat-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}

.chat-head h2 { margin-bottom: 0; }

.chat-count {
  font-size: 12px;
  color: var(--muted);
}

.chat-messages {
  display: flex;
  flex-direction: column;
  gap: 6px;
  height: min(300px, 45vh);
  overflow-y: auto;
  background: #0a0c0f;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  scrollbar-width: thin;
}

.chat-msg {
  font-size: 14px;
  line-height: 1.45;
  word-break: break-word;
}

.chat-msg .who {
  font-weight: 600;
  color: var(--accent);
  margin-right: 6px;
}

/* Your own messages: same weight, but in the "all good" colour so they're
   findable in a busy chat without shouting. */
.chat-msg.self .who { color: var(--ok); }

.chat-msg .text { color: var(--fg); }

.chat-empty {
  color: var(--muted);
  font-size: 13px;
}

.chat-compose {
  display: flex;
  gap: 10px;
  margin-top: 10px;
  flex-wrap: wrap;
}

.chat-compose input { flex: 1; min-width: 130px; }

.chat-compose button { flex: none; }

.chat-status {
  margin: 8px 0 0;
  font-size: 13px;
  color: var(--warn);
  min-height: 1.4em;
}

/* ---- per-viewer latency table ---- */

.viewers {
  margin-top: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  /* Long broadcasts can gather more viewers than fit on screen; scroll the list
     rather than the page, and keep the worst offenders (sorted first) visible. */
  max-height: 260px;
  overflow-y: auto;
}

.viewers .vrow {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  gap: 10px;
  align-items: center;
  padding: 8px 12px;
  border-bottom: 1px solid var(--line);
  font-family: var(--mono);
  font-size: 13px;
}

.viewers .vrow:last-child { border-bottom: 0; }

.viewers .vdev {
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.viewers .vtransport {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}

.viewers .vms { font-variant-numeric: tabular-nums; }
.viewers .vms.good { color: var(--ok); }
.viewers .vms.warn { color: var(--warn); }
.viewers .vms.bad { color: var(--live); }

.viewers .empty {
  padding: 14px 12px;
  color: var(--muted);
  font-size: 13px;
}

/* ---- messages ---- */

.msg {
  padding: 11px 14px;
  border-radius: 8px;
  font-size: 14px;
  margin-top: 14px;
  border: 1px solid;
  white-space: pre-wrap;
}
.msg[hidden] { display: none; }
.msg.error { background: rgba(255, 71, 87, 0.1); border-color: rgba(255, 71, 87, 0.4); color: #ff9aa3; }
.msg.warn  { background: rgba(255, 176, 32, 0.1); border-color: rgba(255, 176, 32, 0.4); color: #ffca6b; }
.msg.info  { background: rgba(79, 195, 247, 0.1); border-color: rgba(79, 195, 247, 0.35); color: #9bdcfb; }

.msg code {
  font-family: var(--mono);
  font-size: 12.5px;
  background: rgba(0, 0, 0, 0.35);
  padding: 1px 5px;
  border-radius: 4px;
}

/* ---- login ---- */

.login {
  max-width: 330px;
  margin: 60px auto;
}

.login .field { min-width: 0; }

.login button { width: 100%; margin-top: 4px; }

.hint {
  color: var(--muted);
  font-size: 13px;
  margin: 10px 0 0;
}

video::-webkit-media-controls-timeline { display: none; }

/* ---- diagnostics log ---- */

.log {
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.55;
  background: #0a0c0f;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  height: 230px;
  overflow-y: auto;
  white-space: pre-wrap;
  word-break: break-word;
}

/* ---- controls row ---- */

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 12px;
}

.controls button { flex: 1 1 auto; min-width: 120px; }

/* ---- tap to play (iOS autoplay refusal) ---- */

.tap-to-play {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  background: rgba(11, 13, 16, 0.72);
  border: 0;
  border-radius: 0;
  color: var(--fg);
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  z-index: 3;
}

.tap-to-play[hidden] { display: none; }

.play-glyph {
  display: grid;
  place-content: center;
  width: 72px;
  height: 72px;
  border-radius: 50%;
  background: var(--accent);
  color: #06212c;
  font-size: 30px;
  padding-left: 5px;
}

/* ---- share / QR ---- */

.share {
  display: flex;
  gap: 18px;
  align-items: flex-start;
  flex-wrap: wrap;
}

.qr {
  width: 180px;
  height: 180px;
  border-radius: 8px;
  background: #fff;
  padding: 8px;
  display: block;
  flex: none;
}

.share-info { flex: 1; min-width: 200px; }

.share-url {
  font-family: var(--mono);
  font-size: 14px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 10px 12px;
  margin-bottom: 10px;
  word-break: break-all;
  /* Let people long-press to select on iOS. */
  -webkit-user-select: all;
  user-select: all;
}

/* ---- pointerless devices: touch screens and Apple Vision Pro ----
   visionOS targets by gaze + pinch, so there is no hover state to rely on and
   small targets are hard to acquire. Give explicit press feedback and roomier
   hit areas rather than leaning on :hover. */

@media (hover: none), (pointer: coarse) {
  button, select { min-height: 48px; }

  button:active {
    transform: scale(0.97);
    background: #2b333d;
  }

  button.primary:active { background: #8ad9fb; }

  /* Generous focus ring — on visionOS this is the main "where am I" cue. */
  button:focus-visible, select:focus-visible, input:focus-visible {
    outline: 3px solid var(--accent);
    outline-offset: 2px;
  }
}

/* ---- stream windows ----
   Every live stream that isn't on the big stage gets a window here, arranged in
   a wrap-around mosaic. Windows are freely resizable by dragging the corner
   handle; clicking one promotes it to the stage. */

.windows { margin-bottom: 18px; }

.window-list {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
}

.window {
  position: relative;
  flex: 0 0 auto;
  width: 360px;
  height: 203px;   /* 16:9 for the width above */
  background: #000;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  cursor: pointer;
}

.window:hover { border-color: var(--accent); }

.window video {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  background: #000;
}

.window-title {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 18px 12px 7px;
  font-size: 12px;
  font-weight: 600;
  color: var(--fg);
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.75));
  pointer-events: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.window-count {
  position: absolute;
  top: 6px;
  right: 6px;
  padding: 1px 7px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  background: rgba(0, 0, 0, 0.6);
  color: var(--muted);
  pointer-events: none;
}

.window-count::before { content: "👁 "; }

/* A connected window gets a live dot so "not started playing yet" is distinct
   from "playing". */
.window.is-live .window-title::after {
  content: "";
  display: inline-block;
  width: 7px;
  height: 7px;
  margin-left: 6px;
  border-radius: 50%;
  background: var(--live);
  vertical-align: 1px;
  animation: pulse 1.6s ease-in-out infinite;
}

/* Corner drag handle for resizing. touch-action: none keeps the drag from being
   interpreted as a page scroll on touch devices. */
.resize-handle {
  position: absolute;
  right: 0;
  bottom: 0;
  width: 20px;
  height: 20px;
  cursor: nwse-resize;
  touch-action: none;
}

.resize-handle::before {
  content: "";
  position: absolute;
  right: 3px;
  bottom: 3px;
  width: 9px;
  height: 9px;
  border-right: 2px solid rgba(232, 237, 243, 0.45);
  border-bottom: 2px solid rgba(232, 237, 243, 0.45);
}

/* ---- phones ---- */

@media (max-width: 640px) {
  header { padding-left: 14px; padding-right: 14px; gap: 10px; }
  header h1 { font-size: 16px; }
  main { padding: 14px 14px 48px; }

  /* Edge-to-edge video reads better on a small screen. */
  .stage {
    border-radius: 0;
    border-left: 0;
    border-right: 0;
    margin-left: -14px;
    margin-right: -14px;
    width: calc(100% + 28px);
  }

  .panel { padding: 14px; }
  .stats { grid-template-columns: repeat(auto-fit, minmax(88px, 1fr)); gap: 8px; }
  .stat .v { font-size: 17px; }
  .controls button { flex: 1 1 100%; }
  .chat-compose { flex-direction: column; }
  .chat-compose input, .chat-compose button { width: 100%; }
  .share { justify-content: center; }
  .qr { width: 200px; height: 200px; margin: 0 auto; }
  .share-info { min-width: 100%; }
  .log { height: 180px; font-size: 11px; }
  .window { width: 100% !important; height: auto !important; aspect-ratio: 16 / 9; }
  .resize-handle { display: none; }
}

/* In landscape on a phone, give the video the whole screen. */
@media (max-width: 900px) and (orientation: landscape) {
  header { padding-top: calc(8px + env(safe-area-inset-top)); padding-bottom: 8px; }
  main { padding-top: 10px; }
}

.logline { color: var(--muted); }
.logline.info { color: #9fb0c2; }
.logline.warn { color: var(--warn); }
.logline.error { color: #ff7b86; }
