:root {
  --bg: #f8fafc;
  --panel: #ffffff;
  --text: #1e293b;
  --muted: #64748b;
  --accent: #0ea5e9;
  --accent-hover: #0284c7;
  --danger: #ef4444;
  --ring: #e2e8f0;
  --ring-dark: #cbd5e1;
  --shadow: rgba(15, 23, 42, 0.08);
  --shadow-lg: rgba(15, 23, 42, 0.12);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: linear-gradient(135deg, #f1f5f9 0%, #e2e8f0 100%);
  color: var(--text);
  font-family: "Inter", system-ui, -apple-system, Segoe UI, Roboto, Ubuntu, Cantarell, "Helvetica Neue", Arial, "Noto Sans", sans-serif;
}

.container {
  max-width: 1100px;
  margin: 0 auto;
  padding: 20px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--panel);
  border: 1px solid var(--ring);
  border-radius: 16px;
  padding: 14px 20px;
  margin-bottom: 16px;
  box-shadow: 0 1px 3px var(--shadow), 0 1px 2px var(--shadow);
}

.topbar h1 {
  font-size: 20px;
  font-weight: 600;
  margin: 0;
  background: linear-gradient(135deg, var(--accent) 0%, #6366f1 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.room {
  display: flex;
  gap: 12px;
  align-items: center;
}

#roomLabel {
  color: var(--muted);
  font-size: 14px;
}

#roomId {
  background: #f1f5f9;
  padding: 6px 12px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  border: 1px solid var(--ring);
}

button {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--ring);
  padding: 10px 16px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.15s ease;
  box-shadow: 0 1px 2px var(--shadow);
}

button:hover {
  border-color: var(--accent);
  background: #f8fafc;
  box-shadow: 0 2px 4px var(--shadow);
}

button:active {
  transform: translateY(1px);
}

button.danger {
  border-color: var(--danger);
  color: var(--danger);
}

button.danger:hover {
  background: #fef2f2;
  border-color: var(--danger);
}

.videos {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.videoWrap {
  position: relative;
  background: var(--panel);
  border: 1px solid var(--ring);
  border-radius: 14px;
  overflow: hidden;
  min-height: 280px;
}

video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  background: linear-gradient(180deg, #1e293b 0%, #0f172a 100%);
}

.label {
  position: absolute;
  left: 10px;
  bottom: 10px;
  font-size: 12px;
  color: #fff;
  background: rgba(15, 23, 42, 0.6);
  backdrop-filter: blur(8px);
  border-radius: 8px;
  padding: 4px 10px;
  font-weight: 500;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin: 16px 0;
  justify-content: center;
}

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

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

/* Fullstage layout */
.stage {
  position: relative;
  width: 100%;
  min-height: calc(100vh - 160px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  background: transparent;
}

/* Remote = full video, no frame */
.remote {
  max-width: 100%;
  max-height: calc(100vh - 240px);
  width: auto;
  height: auto;
  object-fit: contain;
  border-radius: 20px;
  box-shadow: 0 4px 6px var(--shadow), 0 10px 20px var(--shadow-lg);
}

/* Controls inside stage, below video */
.stage .controls {
  margin-top: 16px;
}

/* Picture-in-picture (your feed) */
.pip {
  position: fixed;
  right: 24px;
  bottom: 24px;
  width: 280px;
  min-width: 120px;
  max-width: 50vw;
  aspect-ratio: 16 / 9;
  border: 2px solid rgba(255, 255, 255, 0.15);
  border-radius: 16px;
  overflow: hidden;
  background: linear-gradient(180deg, #1e293b 0%, #0f172a 100%);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3), 0 2px 8px rgba(0, 0, 0, 0.2);
  z-index: 999;
  transition: box-shadow 0.2s ease;
}

.pip:hover {
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.35), 0 4px 12px rgba(0, 0, 0, 0.25);
}

/* Resize handle for PiP */
.pip-resize-handle {
  position: absolute;
  top: 0;
  left: 0;
  width: 20px;
  height: 20px;
  cursor: nwse-resize;
  background: linear-gradient(135deg, rgba(255,255,255,0.3) 0%, transparent 50%);
  border-top-left-radius: 14px;
  opacity: 0;
  transition: opacity 0.2s ease;
}

.pip:hover .pip-resize-handle {
  opacity: 1;
}

.pip-resize-handle::before {
  content: '';
  position: absolute;
  top: 4px;
  left: 4px;
  width: 8px;
  height: 8px;
  border-top: 2px solid rgba(255,255,255,0.6);
  border-left: 2px solid rgba(255,255,255,0.6);
}

.pip.resizing {
  transition: none;
}

/* Fullscreen mode (document fullscreen) */
body.fullscreen-mode .container {
  max-width: none;
  padding: 0;
}

body.fullscreen-mode .topbar {
  display: none;
}

body.fullscreen-mode .stage {
  position: fixed;
  inset: 0;
  background: #000;
  justify-content: center;
  padding: 0;
  z-index: 1;
}

body.fullscreen-mode .remote {
  width: 100vw;
  height: 100vh;
  max-width: none;
  max-height: none;
  object-fit: cover;
  border-radius: 0;
  box-shadow: none;
}

.stage:fullscreen .remote,
.stage.is-fullscreen .remote {
  max-width: 100vw;
  max-height: calc(100vh - 80px);
  border-radius: 0;
  box-shadow: none;
}

body.fullscreen-mode .controls {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  margin: 0;
  padding: 16px;
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.8));
  justify-content: center;
  opacity: 0;
  transition: opacity 0.3s ease;
  z-index: 30;
}

body.fullscreen-mode:hover .controls,
body.fullscreen-mode .controls:focus-within,
.stage:fullscreen:hover .controls,
.stage.is-fullscreen:hover .controls,
.stage:fullscreen .controls:focus-within,
.stage.is-fullscreen .controls:focus-within {
  opacity: 1;
}

body.fullscreen-mode .controls button,
.stage:fullscreen .controls button,
.stage.is-fullscreen .controls button {
  background: rgba(255, 255, 255, 0.15);
  border-color: rgba(255, 255, 255, 0.3);
  color: #fff;
  backdrop-filter: blur(8px);
}

body.fullscreen-mode .controls button:hover,
.stage:fullscreen .controls button:hover,
.stage.is-fullscreen .controls button:hover {
  background: rgba(255, 255, 255, 0.25);
  border-color: rgba(255, 255, 255, 0.5);
}

body.fullscreen-mode .controls button.danger,
.stage:fullscreen .controls button.danger,
.stage.is-fullscreen .controls button.danger {
  background: rgba(239, 68, 68, 0.3);
  border-color: rgba(239, 68, 68, 0.5);
  color: #fca5a5;
}

body.fullscreen-mode .controls button.danger:hover,
.stage:fullscreen .controls button.danger:hover,
.stage.is-fullscreen .controls button.danger:hover {
  background: rgba(239, 68, 68, 0.5);
}

body.fullscreen-mode .pip {
  position: fixed;
  right: 24px;
  bottom: 100px;
  left: auto;
  top: auto;
  z-index: 40;
  width: 240px;
  display: block;
}

body.fullscreen-mode .toy-panel {
  position: fixed;
  left: 24px;
  bottom: 100px;
  right: auto;
  top: auto;
  z-index: 50;
  max-height: calc(100vh - 180px);
}

body.fullscreen-mode .toy-panel.minimized {
  bottom: 24px;
}

.local {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.pipLabel {
  position: absolute;
  left: 10px;
  bottom: 8px;
  font-size: 12px;
  color: #fff;
  background: rgba(15, 23, 42, 0.6);
  backdrop-filter: blur(8px);
  border-radius: 8px;
  padding: 4px 10px;
  font-weight: 500;
}

/* Hide old grid if it still exists */
.videos, .videoWrap { display: none; }

/* Responsive: shrink PiP a bit on small screens */
@media (max-width: 640px) {
  .container { padding: 12px; }
  .stage {
    height: calc(100vh - 240px);
    border-radius: 16px;
  }
  .pip {
    width: 160px;
    max-width: 40vw;
    right: 12px;
    bottom: 12px;
    border-radius: 12px;
  }
  .topbar {
    flex-direction: column;
    gap: 12px;
    text-align: center;
  }
  .controls {
    gap: 8px;
  }
  button {
    padding: 10px 14px;
    font-size: 13px;
  }
}

/* Recording indicator */
.recording-indicator {
  position: fixed;
  top: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: linear-gradient(135deg, rgba(239, 68, 68, 0.95) 0%, rgba(220, 38, 38, 0.95) 100%);
  color: white;
  padding: 12px 24px;
  border-radius: 14px;
  font-size: 14px;
  font-weight: 600;
  z-index: 1000;
  display: none;
  align-items: center;
  gap: 12px;
  box-shadow: 0 4px 12px rgba(239, 68, 68, 0.35), 0 8px 24px rgba(239, 68, 68, 0.2);
  backdrop-filter: blur(8px);
}

.recording-indicator.active {
  display: flex;
}

.recording-dot {
  width: 12px;
  height: 12px;
  background: white;
  border-radius: 50%;
  animation: pulse 1.5s ease-in-out infinite;
  box-shadow: 0 0 8px rgba(255, 255, 255, 0.5);
}

@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.5; transform: scale(0.9); }
}

/* Toy Panel (hidden by default) */
.toy-panel {
  display: none;
  position: fixed;
  left: 24px;
  bottom: 24px;
  width: 380px;
  max-width: calc(100vw - 48px);
  max-height: calc(100vh - 100px);
  background: linear-gradient(135deg, #fff0f7 0%, #ffe3f1 100%);
  border: 1px solid rgba(255, 64, 129, 0.25);
  border-radius: 18px;
  box-shadow: 0 18px 50px rgba(255, 47, 122, 0.25);
  z-index: 1000;
  overflow: hidden;
  font-family: "Space Grotesk", system-ui, sans-serif;
}

.toy-panel.active {
  display: block;
}

.toy-panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 16px;
  background: rgba(255, 255, 255, 0.5);
  border-bottom: 1px solid rgba(255, 64, 129, 0.15);
  font-weight: 600;
  color: #2b1120;
}

.toy-header-buttons {
  display: flex;
  gap: 8px;
  align-items: center;
}

.toy-minimize,
.toy-close {
  background: transparent;
  border: none;
  font-size: 24px;
  color: #7d4f68;
  cursor: pointer;
  padding: 0;
  line-height: 1;
  box-shadow: none;
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
}

.toy-minimize:hover,
.toy-close:hover {
  color: #ff2f7a;
  background: rgba(255, 47, 122, 0.1);
}

/* Minimized state */
.toy-panel.minimized {
  width: auto;
  min-width: 160px;
}

.toy-panel.minimized .toy-panel-content {
  display: none;
}

.toy-panel.minimized .toy-minimize {
  transform: rotate(180deg);
}

.toy-panel-content {
  padding: 16px;
  overflow-y: auto;
  max-height: calc(100vh - 200px);
}

.toy-row {
  display: flex;
  gap: 10px;
  align-items: center;
  margin: 10px 0;
  flex-wrap: wrap;
}

.toy-label {
  display: flex;
  gap: 6px;
  align-items: center;
  color: #7d4f68;
  font-size: 13px;
}

.toy-panel .toy-easing {
  display: none;
}

.toy-panel .toy-room-name {
  display: none;
}

.toy-label input,
.toy-label select {
  background: rgba(255, 255, 255, 0.6);
  border: 1px solid rgba(255, 64, 129, 0.25);
  color: #2b1120;
  padding: 6px 8px;
  border-radius: 8px;
  outline: none;
  width: 70px;
  font-size: 13px;
}

.toy-label input:focus,
.toy-label select:focus {
  border-color: #ff2f7a;
}

.toy-label input[type="text"] {
  width: auto;
  min-width: 80px;
}

.toy-panel input[type="range"] {
  flex: 1;
  min-width: 120px;
  accent-color: #ff2f7a;
}

.toy-pill {
  background: rgba(255, 255, 255, 0.65);
  padding: 4px 10px;
  border-radius: 999px;
  border: 1px solid rgba(255, 64, 129, 0.25);
  color: #2b1120;
  font-size: 12px;
}

.toy-pill code {
  background: transparent;
  padding: 0;
  border: none;
}

.toy-panel code {
  background: rgba(255, 255, 255, 0.6);
  padding: 3px 6px;
  border-radius: 6px;
  border: 1px solid rgba(255, 64, 129, 0.25);
  font-size: 12px;
}

.toy-btn-ghost {
  padding: 6px 12px;
  cursor: pointer;
  border-radius: 8px;
  border: 1px solid rgba(255, 64, 129, 0.25);
  background: transparent;
  color: #2b1120;
  font-weight: 500;
  font-size: 13px;
  box-shadow: none;
}

.toy-btn-ghost:hover {
  border-color: #ff2f7a;
  background: rgba(255, 47, 122, 0.1);
}

.toy-section {
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid rgba(255, 64, 129, 0.15);
}

.toy-status-ok {
  color: #18c29c;
}

/* Make title clickable for secret */
.topbar h1 {
  cursor: default;
  user-select: none;
}

@media (max-width: 640px) {
  .toy-panel {
    left: 12px;
    bottom: 12px;
    width: calc(100vw - 24px);
  }
}
