* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  background: #0f0f12;
  color: #eaeaea;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

header {
  padding: 12px 20px;
  background: #17171c;
  border-bottom: 1px solid #2a2a32;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}
header h1 { font-size: 17px; font-weight: 600; }
header h1 .muted { color: #888; font-weight: 400; }
.count {
  display: inline-block;
  background: #4a90e2;
  color: white;
  font-size: 13px;
  padding: 2px 8px;
  border-radius: 10px;
  margin-left: 6px;
}

.btn, .btn-danger {
  background: #4a90e2;
  color: white;
  padding: 9px 16px;
  text-decoration: none;
  border-radius: 5px;
  border: none;
  font-size: 14px;
  cursor: pointer;
  display: inline-block;
}
.btn:hover { background: #357abd; }
.btn-danger { background: #d9534f; }
.btn-danger:hover { background: #b93532; }

main { flex: 1; display: flex; min-height: 0; position: relative; }

.active-view {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  position: relative;
}

.grid {
  flex: 1;
  display: grid;
  gap: 6px;
  padding: 6px;
  min-height: 0;
  min-width: 0;
}

.tile {
  position: relative;
  background: #000;
  cursor: pointer;
  overflow: hidden;
  border-radius: 6px;
  min-height: 0;
  min-width: 0;
  transition: outline 0.1s;
  outline: 2px solid transparent;
}
.tile:hover { outline-color: #4a90e2; }
.tile video {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  background: #000;
}
.tile .label {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: linear-gradient(transparent, rgba(0,0,0,0.85));
  padding: 18px 12px 10px;
  font-size: 14px;
  font-weight: 500;
  pointer-events: none;
}
.tile .expand-hint {
  position: absolute;
  top: 8px;
  right: 8px;
  background: rgba(0,0,0,0.6);
  color: #fff;
  font-size: 12px;
  padding: 4px 8px;
  border-radius: 4px;
  opacity: 0;
  transition: opacity 0.15s;
  pointer-events: none;
}
.tile:hover .expand-hint { opacity: 1; }

.conn-status {
  position: absolute;
  top: 8px;
  left: 8px;
  background: rgba(0,0,0,0.75);
  color: #aaa;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 11px;
  font-family: ui-monospace, "Cascadia Mono", Consolas, monospace;
  pointer-events: none;
  max-width: 70%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.conn-status.state-new,
.conn-status.state-checking { color: #f0c674; }
.conn-status.state-connected,
.conn-status.state-completed { color: #88d36a; }
.conn-status.state-failed,
.conn-status.state-disconnected,
.conn-status.state-closed { color: #ff7070; }

.conn-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  background: rgba(0,0,0,0.7);
  color: #eee;
  font-size: 17px;
  text-align: center;
  padding: 20px;
  pointer-events: none;
  transition: opacity 0.2s;
}
.conn-overlay.hidden { display: none; }
.conn-overlay .spinner {
  width: 44px;
  height: 44px;
  border: 4px solid rgba(255,255,255,0.15);
  border-top-color: #4a90e2;
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}
.conn-overlay .sub {
  font-size: 13px;
  color: #aaa;
  max-width: 260px;
}
.conn-overlay.error .spinner {
  display: none;
}
.conn-overlay.error {
  color: #ff8a8a;
}
@keyframes spin {
  to { transform: rotate(360deg); }
}

.empty {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #666;
  font-size: 17px;
  padding: 40px;
  text-align: center;
}

/* Self preview overlay (streamer's own view) */
.self-preview {
  position: fixed;
  bottom: 16px;
  left: 16px;
  width: 280px;
  background: #000;
  border-radius: 6px;
  overflow: hidden;
  box-shadow: 0 6px 24px rgba(0,0,0,0.7);
  border: 1px solid #333;
  z-index: 50;
  display: flex;
  flex-direction: column;
}
.self-preview video {
  width: 100%;
  aspect-ratio: 16 / 9;
  object-fit: contain;
  background: #000;
  display: block;
}
.self-preview.minimized video { display: none; }
.self-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 10px;
  background: #1a1a1a;
  font-size: 12px;
  color: #ccc;
  gap: 8px;
}
.self-header .track-info {
  color: #888;
  margin-left: 4px;
}
.self-header button {
  background: transparent;
  color: #aaa;
  border: none;
  cursor: pointer;
  font-size: 18px;
  padding: 0 4px;
  line-height: 1;
}
.self-header button:hover { color: white; }

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.96);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
}
.modal.hidden { display: none; }
.modal video {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}
#modal-title {
  position: absolute;
  top: 16px;
  left: 20px;
  font-size: 16px;
  font-weight: 500;
  background: rgba(0,0,0,0.7);
  padding: 8px 14px;
  border-radius: 5px;
}
#close-modal {
  position: absolute;
  top: 16px;
  right: 20px;
  background: rgba(0,0,0,0.7);
  color: white;
  border: none;
  font-size: 26px;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  cursor: pointer;
  line-height: 1;
}
#close-modal:hover { background: rgba(217,83,79,0.85); }

/* Stream setup page */
.stream-page {
  max-width: 620px;
  margin: 0 auto;
  padding: 50px 24px 24px;
  width: 100%;
  overflow-y: auto;
  flex: 1;
}
.stream-page h1 { margin-bottom: 10px; }
.stream-page p { margin: 10px 0; line-height: 1.5; color: #ccc; }
.stream-page .hint { color: #888; font-size: 13px; margin-top: 14px; }
.form-grid {
  display: grid;
  gap: 16px;
  margin: 22px 0 24px;
}
.form-grid label {
  display: grid;
  gap: 6px;
}
.form-grid label > span {
  font-size: 13px;
  color: #aaa;
  font-weight: 500;
}
.form-grid input,
.form-grid select {
  padding: 10px 12px;
  font-size: 15px;
  border-radius: 5px;
  border: 1px solid #333;
  background: #1d1d23;
  color: #eee;
  width: 100%;
  font-family: inherit;
}
.form-grid input:focus,
.form-grid select:focus {
  border-color: #4a90e2;
  outline: none;
}
.btn-start {
  padding: 12px 24px;
  font-size: 16px;
  background: #4a90e2;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  font-weight: 500;
}
.btn-start:hover { background: #357abd; }
.stream-page .back { margin-top: 30px; }
.stream-page a { color: #4a90e2; text-decoration: none; }
.stream-page a:hover { text-decoration: underline; }
