:root {
  --bg: #0d0f12;
  --panel: #161a20;
  --fg: #e8ebf0;
  --muted: #8b95a4;
  --accent: #4ec9b0;
  --danger: #e06c75;
  --border: #232932;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  min-height: 100vh;
}

body {
  display: flex;
  flex-direction: column;
}

header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

header h1 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}

header .room-tag {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 14px;
  color: var(--accent);
  padding: 4px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
}

main {
  flex: 1;
  padding: 20px;
  max-width: 1400px;
  width: 100%;
  margin: 0 auto;
}

.center {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 20px;
  min-height: 70vh;
  text-align: center;
}

.center h2 {
  margin: 0;
  font-size: 22px;
}

.center p {
  color: var(--muted);
  max-width: 400px;
  margin: 0;
}

button, .btn {
  font: inherit;
  background: var(--accent);
  color: #0d0f12;
  border: none;
  padding: 14px 24px;
  border-radius: 10px;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  display: inline-block;
}

button.secondary, .btn.secondary {
  background: var(--panel);
  color: var(--fg);
  border: 1px solid var(--border);
}

button:disabled { opacity: 0.5; cursor: not-allowed; }

input[type=text] {
  font: inherit;
  background: var(--panel);
  color: var(--fg);
  border: 1px solid var(--border);
  padding: 14px 16px;
  border-radius: 10px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 18px;
  letter-spacing: 2px;
  text-transform: uppercase;
  width: 220px;
  text-align: center;
}

.row { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; justify-content: center; }

.code-display {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 48px;
  letter-spacing: 8px;
  color: var(--accent);
  padding: 16px 24px;
  border: 2px dashed var(--border);
  border-radius: 12px;
}

.qr-wrap {
  background: white;
  padding: 16px;
  border-radius: 12px;
  display: inline-block;
}

#qr canvas, #qr img { display: block; }

.viewer-layout {
  display: grid;
  grid-template-columns: 280px 1fr;
  gap: 20px;
  align-items: start;
}

@media (max-width: 800px) {
  .viewer-layout { grid-template-columns: 1fr; }
}

.join-panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-items: center;
  text-align: center;
  position: sticky;
  top: 20px;
}

.join-panel .qr-wrap { padding: 8px; }
.join-panel .code-display {
  font-size: 28px;
  letter-spacing: 4px;
  padding: 8px 16px;
}
.small { font-size: 12px; color: var(--muted); margin: 0; word-break: break-all; }

.grid-area { min-height: 200px; }
.empty-msg {
  padding: 40px;
  text-align: center;
  color: var(--muted);
  border: 1px dashed var(--border);
  border-radius: 12px;
}

.video-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 16px;
}

.tile {
  position: relative;
  background: #000;
  border-radius: 12px;
  overflow: hidden;
  aspect-ratio: 16 / 9;
}

.tile video,
.tile canvas.delayed {
  width: 100%;
  height: 100%;
  object-fit: cover;
  background: #000;
  position: absolute;
  inset: 0;
}

body.fit-contain .tile canvas.delayed,
body.fit-contain .tile video.delayed,
body.fit-contain .tile video.live-pip.main {
  object-fit: contain;
}

body.hide-stats .tile .stats { display: none; }

.tile video.live-pip {
  width: 30%;
  height: auto;
  aspect-ratio: 16 / 9;
  top: auto;
  bottom: 8px;
  right: 8px;
  inset: auto 8px 8px auto;
  border-radius: 6px;
  border: 1px solid var(--border);
  z-index: 2;
  object-fit: cover;
  background: #000;
}

.tile video.live-pip.main {
  width: 100%;
  height: 100%;
  inset: 0;
  border-radius: 0;
  border: none;
  z-index: 1;
}

.tile video.delayed,
.tile canvas.delayed { z-index: 1; }
.tile:not(.delayed-on) video.delayed,
.tile:not(.delayed-on) canvas.delayed { display: none; }

.controls .checkbox-row {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
}

body.fs-mode {
  background: #000;
}
body.fs-mode header,
body.fs-mode .join-panel,
body.fs-mode .empty-msg,
body.fs-mode .tile .live-pip,
body.fs-mode .tile .stats,
body.fs-mode .tile .label,
body.fs-mode .tile .delay-badge {
  display: none !important;
}
body.fs-mode .viewer-layout {
  display: block;
  padding: 0;
  margin: 0;
  max-width: none;
}
body.fs-mode main {
  padding: 0;
  margin: 0;
  max-width: none;
}
body.fs-mode .grid-area {
  height: 100vh;
  width: 100vw;
}
body.fs-mode .video-grid {
  gap: 4px;
  padding: 4px;
  height: 100%;
  align-content: stretch;
  grid-auto-rows: 1fr;
}
body.fs-mode .tile {
  aspect-ratio: auto;
  height: 100%;
  border-radius: 0;
}
body.fs-mode .tile video.delayed,
body.fs-mode .tile canvas.delayed {
  display: block;
}

#fullscreen-btn {
  margin-top: 4px;
}

.capture-btn {
  background: #e06c75;
  color: #0d0f12;
  margin-top: 4px;
}
.capture-btn.ok { background: var(--accent); }

.floating-capture {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(224, 108, 117, 0.92);
  color: #0d0f12;
  border: none;
  padding: 14px 28px;
  border-radius: 999px;
  font-weight: 700;
  font-size: 16px;
  cursor: pointer;
  box-shadow: 0 8px 24px rgba(0,0,0,0.5);
  z-index: 1000;
  display: none;
}
.floating-capture .fc-icon {
  color: #fff;
  margin-right: 6px;
  animation: pulse 1.5s infinite;
}
.floating-capture.ok { background: var(--accent); }

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

body.fs-mode .floating-capture { display: inline-block; }

.clips-panel {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 4px;
  border-top: 1px solid var(--border);
  padding-top: 12px;
}

.clips-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 13px;
  color: var(--muted);
  font-weight: 600;
}

.clips-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-height: 260px;
  overflow-y: auto;
  font-size: 12px;
  color: var(--muted);
  text-align: left;
}

.clip-row {
  display: grid;
  grid-template-columns: 1fr auto auto auto;
  gap: 6px;
  align-items: center;
  padding: 4px 6px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
}

.clip-name {
  color: var(--accent);
  text-decoration: none;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.clip-size {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11px;
}

.link-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--fg);
  padding: 2px 6px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
}
.link-btn:hover { border-color: var(--accent); color: var(--accent); }

.tile .label {
  position: absolute;
  top: 8px;
  left: 8px;
  background: rgba(0,0,0,0.6);
  padding: 4px 10px;
  border-radius: 6px;
  font-size: 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

.tile .delay-badge {
  position: absolute;
  top: 8px;
  right: 8px;
  background: rgba(78, 201, 176, 0.85);
  color: #0d0f12;
  padding: 4px 10px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 600;
  z-index: 3;
}

.tile .stats {
  position: absolute;
  left: 8px;
  bottom: 8px;
  background: rgba(0,0,0,0.65);
  color: var(--fg);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11px;
  padding: 6px 8px;
  border-radius: 6px;
  display: grid;
  gap: 2px;
  z-index: 3;
  max-width: 60%;
}

.tile .stats .stat-ok { color: var(--accent); }
.tile .stats .stat-warn { color: #e5c07b; }
.tile .stats .stat-err { color: var(--danger); }

.controls {
  margin-top: 20px;
  display: flex;
  gap: 16px;
  align-items: center;
  flex-wrap: wrap;
}

.controls label {
  color: var(--muted);
  font-size: 14px;
}

#cam-video {
  width: 100%;
  max-width: 480px;
  border-radius: 12px;
  background: #000;
}

.status {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 13px;
  color: var(--muted);
  padding: 8px 12px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  min-height: 16px;
}

.error { color: var(--danger); }

.viewers {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 100%;
  max-width: 480px;
}

.viewer-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  display: grid;
  gap: 3px;
  text-align: left;
}

.viewer-card .vc-head {
  color: var(--accent);
  font-weight: 600;
  margin-bottom: 2px;
}

.viewer-card .stat-ok { color: var(--accent); }
.viewer-card .stat-warn { color: #e5c07b; }
.viewer-card .stat-err { color: var(--danger); }

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