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

body {
  margin: 0;
  font-family: "Segoe UI", system-ui, sans-serif;
  background: #121212;
  color: #e0e0e0;
  display: flex;
  height: 100vh;
}

.sidebar {
  width: 300px;
  flex-shrink: 0;
  background: #1e1e1e;
  padding: 20px;
  box-shadow: 2px 0 10px rgba(0, 0, 0, 0.5);
  z-index: 10;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  overflow-x: hidden;
  transition: margin-left 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.sidebar.collapsed {
  margin-left: -300px;
}

.sidebar h2 {
  margin-top: 0;
  color: #ffffff;
  font-size: 1.2rem;
  border-bottom: 1px solid #333;
  padding-bottom: 15px;
  margin-bottom: 15px;
}

.help-text {
  font-size: 0.8rem;
  color: #888;
  margin-top: -10px;
  margin-bottom: 20px;
  font-style: italic;
}

ul.stream-list {
  list-style-type: none;
  padding: 0;
  margin: 0 0 30px 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

li.stream-item {
  display: flex;
  gap: 8px;
}

.stream-btn {
  flex-grow: 1;
  padding: 12px 15px;
  background: #2c2c2c;
  color: #fff;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  text-align: left;
  font-size: 1rem;
  transition: all 0.2s ease;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  /* Disable text selection so shift-clicking doesn't highlight text */
  user-select: none;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.stream-btn:hover {
  background: #3d3d3d;
  transform: translateX(4px);
}

.stream-btn.active {
  background: #0d6efd;
  box-shadow: 0 4px 12px rgba(13, 110, 253, 0.4);
  transform: none;
}

.stream-stats {
  display: flex;
  gap: 8px;
  align-items: center;
  font-size: 0.75rem;
  color: #888;
}

/* Viewer Count and Live Dot */
.viewer-count {
  display: none;
}
.viewer-count.active {
  display: block;
}

.live-dot {
  width: 8px;
  height: 8px;
  background: #ff4444;
  border-radius: 50%;
  display: none;
  box-shadow: 0 0 5px #ff4444;
  animation: pulse-red 2s infinite;
}
.live-dot.active {
  display: block;
}

@keyframes pulse-red {
  0% {
    transform: scale(0.95);
    opacity: 0.8;
  }
  50% {
    transform: scale(1.2);
    opacity: 1;
  }
  100% {
    transform: scale(0.95);
    opacity: 0.8;
  }
}

.delete-btn {
  background: transparent;
  color: #888;
  border: 1px solid #444;
  border-radius: 8px;
  padding: 0 12px;
  cursor: pointer;
  font-size: 1.2rem;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.delete-btn:hover {
  background: #ff4444;
  color: #fff;
  border-color: #ff4444;
}

.add-stream-form fieldset {
  border: 1px solid #333;
  border-radius: 8px;
  padding: 15px;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.add-stream-form legend {
  color: #aaa;
  font-size: 0.9rem;
  padding: 0 5px;
}

.add-stream-form input {
  width: 100%;
  background: #121212;
  border: 1px solid #444;
  color: #fff;
  padding: 10px;
  border-radius: 6px;
  font-size: 0.9rem;
}

.add-stream-form input:focus {
  outline: none;
  border-color: #0d6efd;
}

.add-stream-form button {
  width: 100%;
  background: #198754;
  color: #fff;
  border: none;
  padding: 10px;
  border-radius: 6px;
  cursor: pointer;
  font-weight: bold;
  transition: background 0.2s;
}

.add-stream-form button:hover {
  background: #157347;
}

.main-content {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  padding: 20px;
  gap: 15px;
  min-width: 0;
}

/* Unified Dynamic Grid Container */
.video-grid {
  flex-grow: 1;
  display: grid;
  gap: 15px;
  border-radius: 12px;
  overflow-y: auto;
  padding-right: 5px;
  grid-auto-rows: 1fr;

  /* Fallback if there's more streams than explicitly defined below */
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
}

/* 1 Stream -> 1 Column (1x1) */
.video-grid:has(> .grid-cell:nth-last-child(1):first-child) {
  grid-template-columns: 1fr;
}

/* 2 to 4 Streams -> 2 Columns (2x1 or 2x2) */
.video-grid:has(
  > .grid-cell:nth-last-child(n + 2):nth-last-child(-n + 4):first-child
) {
  grid-template-columns: repeat(2, 1fr);
}

/* 5 to 9 Streams -> 3 Columns (3x2 or 3x3) */
.video-grid:has(
  > .grid-cell:nth-last-child(n + 5):nth-last-child(-n + 9):first-child
) {
  grid-template-columns: repeat(3, 1fr);
}

/* 10 to 16 Streams -> 4 Columns (4x3 or 4x4) */
.video-grid:has(
  > .grid-cell:nth-last-child(n + 10):nth-last-child(-n + 16):first-child
) {
  grid-template-columns: repeat(4, 1fr);
}

.grid-cell {
  background: #000;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.8);
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 0;
}
.grid-cell:fullscreen {
  border-radius: 0;
  width: 100vw;
  height: 100vh;
  background: #000;
  margin: 0;
}

.grid-label {
  position: absolute;
  top: 10px;
  left: 10px;
  background: rgba(0, 0, 0, 0.7);
  padding: 5px 10px;
  border-radius: 6px;
  font-size: 0.85rem;
  font-weight: 500;
  backdrop-filter: blur(4px);
  pointer-events: none;
  z-index: 2;
}

video {
  width: 100%;
  height: 100%;
  object-fit: contain;

  position: relative;
  z-index: 1;
}

.status-bar {
  padding: 15px 20px;
  border-radius: 8px;
  background: #1e1e1e;
  display: flex;
  align-items: center;
  font-weight: 500;
  flex-shrink: 0;
  justify-content: space-between;
}

.status-bar p {
  margin: 0;
  display: flex;
  align-items: center;
}

.theatre-btn {
  background: transparent;
  color: #aaa;
  border: 1px solid #444;
  padding: 6px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.9rem;
  display: flex;
  align-items: center;
  gap: 6px;
  transition: all 0.2s ease;
}
.theatre-btn:hover {
  color: #fff;
  border-color: #888;
  background: #2c2c2c;
}

.indicator {
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: #ff4444;
  margin-right: 10px;
}

.indicator.live {
  background: #00c851;
  box-shadow: 0 0 10px #00c851;
}

.indicator.connecting {
  background: #ffbb33;
  box-shadow: 0 0 10px #ffbb33;
}

.reset-btn {
  background: transparent;
  color: #888;
  border: 1px dashed #444;
  padding: 8px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.85rem;
  margin-top: 15px;
  width: 100%;
  transition: all 0.2s;
}

.reset-btn:hover {
  color: #ff4444;
  border-color: #ff4444;
  background: rgba(255, 68, 68, 0.1);
}

/* Login Modal Styles */
.login-modal {
  background: #1e1e1e;
  color: #fff;
  border: 1px solid #333;
  border-radius: 12px;
  padding: 30px;
  box-shadow: 0 15px 50px rgba(0, 0, 0, 0.9);
  width: 320px;
}

.login-modal::backdrop {
  background: rgba(0, 0, 0, 0.85);
  backdrop-filter: blur(5px);
}

.login-modal h2 {
  margin-top: 0;
}

.login-modal p {
  color: #aaa;
  font-size: 0.9rem;
  margin-bottom: 20px;
}

.login-modal fieldset {
  border: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.login-modal input {
  background: #121212;
  border: 1px solid #444;
  color: #fff;
  padding: 12px;
  border-radius: 6px;
  font-size: 1rem;
  width: 100%;
}

.login-modal input:focus {
  outline: none;
  border-color: #0d6efd;
}

.login-modal button {
  background: #0d6efd;
  color: #fff;
  border: none;
  padding: 12px;
  border-radius: 6px;
  cursor: pointer;
  font-weight: bold;
  font-size: 1rem;
  transition: background 0.2s;
}

.login-modal button:hover {
  background: #0b5ed7;
}

/* Semantic Settings Form Styles */
.settings-form {
  margin-top: 20px;
}
.settings-form fieldset {
  border: 1px solid #333;
  border-radius: 8px;
  padding: 15px;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.settings-form legend {
  color: #aaa;
  font-size: 0.9rem;
  padding: 0 5px;
}
.settings-form label {
  font-size: 0.85rem;
  color: #ccc;
}
.settings-form .control-group {
  display: flex;
  gap: 10px;
  align-items: center;
}
.settings-form input[type="range"] {
  flex-grow: 1;
  cursor: pointer;
  accent-color: #0d6efd;
  width: 100%;
}
.settings-form input[type="number"] {
  width: 75px;
  background: #121212;
  border: 1px solid #444;
  color: #fff;
  padding: 6px;
  border-radius: 4px;
  font-size: 0.9rem;
  text-align: center;
}
.settings-form input[type="number"]:focus {
  outline: none;
  border-color: #0d6efd;
}
.settings-form small {
  color: #888;
  font-size: 0.8rem;
  font-style: italic;
  margin-top: -2px;
}
