:root {
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: #060606;
  color: #f5f5f5;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
}

main.layout {
  max-width: 960px;
  margin: 0 auto;
  padding: 2rem 1.5rem 4rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

header h1 {
  margin-bottom: 0.5rem;
}

.player-section {
  background: #111;
  border-radius: 1rem;
  padding: 1rem;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
}

video {
  width: 100%;
  border-radius: 0.75rem;
  background: #000;
  aspect-ratio: 16 / 9;
}

/* Debug/embed mode visibility. Default (no mode) = player + status + embed
   button only. mode-debug additionally reveals diagnostics. mode-embed hides
   everything except the video and its chat overlay, for use inside an
   <iframe> on another site. */
.segments,
.swarm-stats {
  display: none;
}

body.mode-debug .segments,
body.mode-debug .swarm-stats {
  display: block;
}

body.mode-embed header,
body.mode-embed .status,
body.mode-embed .embed-row,
body.mode-embed .segments,
body.mode-embed .swarm-stats {
  display: none;
}

body.mode-embed main.layout {
  padding: 0;
  max-width: none;
}

body.mode-embed .player-section {
  border-radius: 0;
  box-shadow: none;
  padding: 0;
}

.embed-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-top: 1rem;
  font-size: 0.85rem;
}

.embed-row button {
  background: #1a1a1a;
  color: #f5f5f5;
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 0.5rem;
  padding: 0.5rem 0.9rem;
  cursor: pointer;
  font-size: 0.85rem;
}

.embed-row button:hover {
  background: #222;
}

#embed-copy-status {
  color: rgba(245, 245, 245, 0.6);
}

/* Player + chat overlay */
.player-wrapper {
  position: relative;
}

/* Outer button = a bigger invisible click target, positioned flush with the
   video's top-right corner. A native <video controls> toggles play/pause on
   any click that lands on the video itself, so a hitbox as small as the
   visible icon meant near-misses would fall through and pause playback —
   this way clicks anywhere in the padded area are caught by the button
   instead of reaching the video underneath. */
.chat-toggle-hitbox {
  position: absolute;
  top: 0;
  right: 0;
  width: 3.75rem;
  height: 3.75rem;
  padding: 0.6rem;
  border: none;
  background: transparent;
  display: flex;
  align-items: flex-start;
  justify-content: flex-end;
  cursor: pointer;
  z-index: 5;
}

/* The visible icon itself — same size/position as before; only the
   clickable area around it grew. */
.chat-toggle-visual {
  position: relative;
  width: 2.25rem;
  height: 2.25rem;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.2);
  color: rgba(245, 245, 245, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s ease, color 0.15s ease;
}

.chat-toggle-hitbox:hover .chat-toggle-visual {
  background: rgba(0, 0, 0, 0.55);
  color: #f5f5f5;
}

/* [hidden] must win over the explicit `display` set below on these — otherwise
   the browser's default `[hidden] { display: none }` rule loses to the more
   specific class selector below and the element stays visible regardless of
   the `hidden` attribute JS toggles. */
.chat-badge[hidden],
.chat-panel[hidden],
.chat-join[hidden],
.chat-main[hidden] {
  display: none;
}

.chat-badge {
  position: absolute;
  top: -0.3rem;
  right: -0.3rem;
  min-width: 1.1rem;
  height: 1.1rem;
  padding: 0 0.25rem;
  border-radius: 0.6rem;
  background: #e74c3c;
  color: #fff;
  font-size: 0.65rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

.chat-panel {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(300px, 85%);
  display: flex;
  flex-direction: column;
  padding: 0.75rem;
  z-index: 4;
}

/* Backdrop lives on its own layer, separate from the actual chat content
   above it, so the left-edge fade (mask) only softens the background/blur
   and never fades out message text that happens to sit near that edge. */
.chat-panel::before {
  content: '';
  position: absolute;
  inset: 0;
  background: rgba(10, 10, 10, 0.32);
  backdrop-filter: blur(1.5px);
  -webkit-mask-image: linear-gradient(to right, transparent, black 35%);
  mask-image: linear-gradient(to right, transparent, black 35%);
  border-radius: 0 0.75rem 0.75rem 0;
  z-index: -1;
  pointer-events: none;
}

.chat-join {
  margin: auto;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  padding: 1rem;
  text-align: center;
}

.chat-join input {
  padding: 0.5rem 0.6rem;
  border-radius: 0.4rem;
  border: 1px solid rgba(255, 255, 255, 0.2);
  background: rgba(255, 255, 255, 0.08);
  color: #f5f5f5;
}

.chat-join button {
  padding: 0.5rem 0.6rem;
  border-radius: 0.4rem;
  border: none;
  background: #2ecc71;
  color: #06210f;
  font-weight: 600;
  cursor: pointer;
}

.chat-main {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.85rem;
  padding-right: 0.25rem;
}

.chat-message {
  word-break: break-word;
  line-height: 1.35;
  font-weight: 600;
  /* Background is mostly see-through, so text needs a hard outline of its
     own against whatever video content is showing behind it — a single soft
     shadow wasn't enough, so this stacks a dark stroke in every direction
     plus a drop shadow (the same trick video subtitles use). */
  text-shadow:
    -1px -1px 1.5px rgba(0, 0, 0, 0.95),
    1px -1px 1.5px rgba(0, 0, 0, 0.95),
    -1px 1px 1.5px rgba(0, 0, 0, 0.95),
    1px 1px 1.5px rgba(0, 0, 0, 0.95),
    0 2px 4px rgba(0, 0, 0, 0.85);
}

.chat-msg-user {
  font-weight: 800;
  /* color assigned per-username via inline style in JS (renderChatMessages) */
}

.chat-form {
  display: flex;
  gap: 0.4rem;
  margin-top: 0.5rem;
}

.chat-form input {
  flex: 1;
  padding: 0.45rem 0.6rem;
  border-radius: 0.4rem;
  border: 1px solid rgba(255, 255, 255, 0.2);
  background: rgba(255, 255, 255, 0.08);
  color: #f5f5f5;
  min-width: 0;
}

.chat-form button {
  padding: 0.45rem 0.75rem;
  border-radius: 0.4rem;
  border: none;
  background: #2ecc71;
  color: #06210f;
  font-weight: 600;
  cursor: pointer;
}

.status {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  margin-top: 1rem;
  font-size: 0.95rem;
}

.segments {
  background: #111;
  border-radius: 1rem;
  padding: 1rem 1.5rem;
}

.segments ul {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-family: "Roboto Mono", monospace;
  font-size: 0.9rem;
}

.segments li {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.35rem 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.segments li:last-child {
  border-bottom: none;
}

.segments li.now-playing {
  background: rgba(46, 204, 113, 0.12);
  border-left: 3px solid #2ecc71;
  padding-left: 0.5rem;
  margin-left: -0.5rem;
  font-weight: 600;
}

.swarm-stats {
  background: #111;
  border-radius: 1rem;
  padding: 1rem 1.5rem;
}

.metrics-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 0.75rem;
  margin: 1rem 0 1.5rem;
}

.metric {
  background: #1a1a1a;
  border-radius: 0.6rem;
  padding: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.metric-value {
  font-size: 1.4rem;
  font-weight: 600;
  font-family: "Roboto Mono", monospace;
}

.metric-label {
  font-size: 0.75rem;
  color: rgba(245, 245, 245, 0.6);
}

.torrent-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
  font-family: "Roboto Mono", monospace;
}

.torrent-table th {
  text-align: left;
  color: rgba(245, 245, 245, 0.6);
  font-weight: 500;
  padding: 0.4rem 0.5rem;
  border-bottom: 1px solid rgba(255, 255, 255, 0.15);
}

.torrent-table td {
  padding: 0.4rem 0.5rem;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  vertical-align: middle;
}

.peer-cell {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
}

.peer-badge {
  display: inline-block;
  padding: 0.1rem 0.45rem;
  border-radius: 1rem;
  font-size: 0.75rem;
  white-space: nowrap;
}

.peer-seeding {
  background: rgba(46, 204, 113, 0.2);
  color: #2ecc71;
}

.peer-leeching {
  background: rgba(241, 196, 15, 0.2);
  color: #f1c40f;
}

.peer-idle {
  background: rgba(255, 255, 255, 0.1);
  color: rgba(245, 245, 245, 0.6);
}

.peer-origin {
  background: rgba(52, 152, 219, 0.2);
  color: #3498db;
}

/* Dashboard / auth pages */

main.layout.narrow {
  max-width: 420px;
}

.card {
  background: #111;
  border-radius: 1rem;
  padding: 1.5rem;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
}

.card + .card {
  margin-top: 1.5rem;
}

.field-group {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  margin-bottom: 1rem;
}

/* Same [hidden]-vs-display bug noted above for the chat elements: without
   this, the WHIP bearer group / embed domains group stay visible even when
   JS sets the hidden attribute. */
.field-group[hidden],
.metrics-grid[hidden] {
  display: none;
}

.field-group label {
  font-size: 0.85rem;
  color: rgba(245, 245, 245, 0.7);
}

.field-group input {
  padding: 0.6rem 0.7rem;
  border-radius: 0.5rem;
  border: 1px solid rgba(255, 255, 255, 0.2);
  background: rgba(255, 255, 255, 0.06);
  color: #f5f5f5;
  font-size: 0.95rem;
}

.field-group textarea {
  padding: 0.6rem 0.7rem;
  border-radius: 0.5rem;
  border: 1px solid rgba(255, 255, 255, 0.2);
  background: rgba(255, 255, 255, 0.06);
  color: #f5f5f5;
  font-size: 0.9rem;
  font-family: "Roboto Mono", monospace;
  resize: vertical;
  min-height: 5rem;
}

.field-group input:focus,
.field-group textarea:focus {
  outline: 2px solid #2ecc71;
  outline-offset: 1px;
}

/* Higher specificity than `.btn { display: inline-flex }` below, so a hidden
   button actually disappears instead of the UA [hidden] rule losing to it —
   same class of bug already noted above for the chat elements. */
.btn[hidden] {
  display: none;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  padding: 0.6rem 1rem;
  border-radius: 0.5rem;
  border: 1px solid rgba(255, 255, 255, 0.15);
  background: #1a1a1a;
  color: #f5f5f5;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
}

.btn:hover {
  background: #222;
}

.btn-primary {
  background: #2ecc71;
  color: #06210f;
  border-color: transparent;
}

.btn-primary:hover {
  background: #27b866;
}

.btn-danger {
  background: transparent;
  color: #e74c3c;
  border-color: rgba(231, 76, 60, 0.4);
}

.btn-danger:hover {
  background: rgba(231, 76, 60, 0.12);
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.form-error {
  color: #e74c3c;
  font-size: 0.85rem;
  min-height: 1.2em;
}

.form-success {
  color: #2ecc71;
  font-size: 0.85rem;
  min-height: 1.2em;
}

.auth-switch {
  margin-top: 1rem;
  font-size: 0.85rem;
  color: rgba(245, 245, 245, 0.6);
  text-align: center;
}

.auth-switch a {
  color: #2ecc71;
}

.dashboard-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 0.5rem;
}

.dashboard-nav h1 {
  margin: 0;
}

.dashboard-subtitle {
  margin: 0.2rem 0 0;
  color: rgba(245, 245, 245, 0.55);
  font-size: 0.9rem;
}

.dashboard-nav .user-email {
  color: rgba(245, 245, 245, 0.6);
  font-size: 0.85rem;
}

.dashboard-stats {
  margin: 0;
}

.checkout-banner {
  margin: 0;
  padding: 0.75rem 1rem;
  border-radius: 0.6rem;
  font-size: 0.9rem;
}

.checkout-banner.success {
  background: rgba(46, 204, 113, 0.12);
  border: 1px solid rgba(46, 204, 113, 0.4);
  color: #2ecc71;
}

.checkout-banner.canceled {
  background: rgba(241, 196, 15, 0.1);
  border: 1px solid rgba(241, 196, 15, 0.35);
  color: #f1c40f;
}

.nav-actions {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.create-stream-form {
  display: flex;
  gap: 0.6rem;
  margin-bottom: 1.5rem;
}

.create-stream-form input {
  flex: 1;
}

.stream-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.stream-row {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.9rem 1rem;
  background: #1a1a1a;
  border-radius: 0.6rem;
  transition: background 0.15s ease;
}

.stream-row:hover {
  background: #202020;
}

.stream-row-avatar {
  flex: none;
  width: 2.25rem;
  height: 2.25rem;
  border-radius: 50%;
  background: rgba(46, 204, 113, 0.15);
  color: #2ecc71;
  font-weight: 700;
  font-size: 0.95rem;
  display: flex;
  align-items: center;
  justify-content: center;
}

.stream-row-info {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  min-width: 0;
  flex: 1;
}

.stream-row-info a {
  color: #f5f5f5;
  font-weight: 600;
  text-decoration: none;
}

.stream-row-info a:hover {
  text-decoration: underline;
}

.stream-row-meta {
  font-size: 0.8rem;
  color: rgba(245, 245, 245, 0.55);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
}

.live-dot {
  width: 0.5rem;
  height: 0.5rem;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.25);
  display: inline-block;
}

.live-dot.is-live {
  background: #2ecc71;
  box-shadow: 0 0 6px rgba(46, 204, 113, 0.8);
}

.live-dot.is-reconnecting {
  background: #f1c40f;
  box-shadow: 0 0 6px rgba(241, 196, 15, 0.8);
}

.protocol-badge {
  display: inline-block;
  padding: 0.05rem 0.5rem;
  border-radius: 1rem;
  background: rgba(255, 255, 255, 0.08);
  color: rgba(245, 245, 245, 0.65);
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.row-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  padding: 0.05rem 0.5rem;
  border-radius: 1rem;
  background: rgba(255, 255, 255, 0.06);
  color: rgba(245, 245, 245, 0.75);
  font-size: 0.75rem;
  font-family: "Roboto Mono", monospace;
  white-space: nowrap;
}

.empty-state {
  text-align: center;
  padding: 2.5rem 1rem;
  color: rgba(245, 245, 245, 0.5);
}

.empty-state-icon {
  font-size: 2rem;
  margin-bottom: 0.5rem;
  opacity: 0.7;
}

.empty-state-title {
  color: #f5f5f5;
  font-weight: 600;
  margin: 0 0 0.3rem;
}

.empty-state .muted {
  max-width: 26rem;
  margin: 0 auto;
}

.breadcrumb {
  font-size: 0.85rem;
  color: rgba(245, 245, 245, 0.55);
  margin-bottom: 0.5rem;
}

.breadcrumb a {
  color: rgba(245, 245, 245, 0.75);
}

.secret-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.secret-value {
  flex: 1;
  font-family: "Roboto Mono", monospace;
  font-size: 0.85rem;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 0.5rem;
  padding: 0.5rem 0.7rem;
  word-break: break-all;
  filter: blur(4px);
  user-select: none;
  transition: filter 0.15s ease;
}

.secret-value.revealed {
  filter: none;
  user-select: text;
}

.detail-actions {
  display: flex;
  gap: 0.6rem;
  margin-top: 1.5rem;
  flex-wrap: wrap;
}

.chat-archive-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  max-height: 60vh;
  overflow-y: auto;
}

.chat-archive-msg {
  padding: 0.5rem 0.7rem;
  background: #1a1a1a;
  border-radius: 0.5rem;
  font-size: 0.9rem;
}

.chat-archive-msg .meta {
  color: rgba(245, 245, 245, 0.5);
  font-size: 0.75rem;
  margin-left: 0.5rem;
}

.section-title {
  font-size: 1rem;
  font-weight: 600;
  margin: 0 0 1rem;
}

.muted {
  color: rgba(245, 245, 245, 0.55);
}

/* Protocol switch (SRT / RTMP) */

.protocol-switch {
  display: inline-flex;
  border-radius: 0.5rem;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.15);
  margin-bottom: 1rem;
}

.protocol-btn {
  padding: 0.5rem 1rem;
  border: none;
  background: #1a1a1a;
  color: rgba(245, 245, 245, 0.7);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
}

.protocol-btn + .protocol-btn {
  border-left: 1px solid rgba(255, 255, 255, 0.15);
}

.protocol-btn.active {
  background: #2ecc71;
  color: #06210f;
}

.protocol-btn:disabled {
  cursor: not-allowed;
  opacity: 0.6;
}

/* Toggle row (chat enabled, etc.) */

.toggle-row {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  cursor: pointer;
  margin-bottom: 0.5rem;
}

.toggle-row input[type='checkbox'] {
  width: 1.1rem;
  height: 1.1rem;
  accent-color: #2ecc71;
}

/* Viewer analytics chart */

.chart-wrap {
  position: relative;
  margin-top: 1rem;
}

#viewers-chart {
  width: 100%;
  height: 180px;
  overflow: visible;
}

.chart-axis-line {
  stroke: rgba(255, 255, 255, 0.1);
  stroke-width: 1;
}

.chart-axis-label {
  fill: rgba(245, 245, 245, 0.5);
  font-size: 10px;
  font-family: "Roboto Mono", monospace;
}

.chart-line {
  fill: none;
  stroke: #2ecc71;
  stroke-width: 2;
  stroke-linejoin: round;
  stroke-linecap: round;
}

.chart-hover-line {
  stroke: rgba(245, 245, 245, 0.3);
  stroke-width: 1;
  pointer-events: none;
}

.chart-current-point {
  fill: #2ecc71;
  stroke: #060606;
  stroke-width: 2;
}

.chart-hover-point {
  fill: #2ecc71;
  stroke: #060606;
  stroke-width: 2;
  pointer-events: none;
}

.chart-hover-target {
  fill: transparent;
  cursor: crosshair;
}

.chart-tooltip {
  position: absolute;
  pointer-events: none;
  background: #1a1a1a;
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 0.4rem;
  padding: 0.4rem 0.6rem;
  font-size: 0.78rem;
  white-space: nowrap;
  transform: translate(-50%, -110%);
  z-index: 2;
}

.chart-tooltip .tooltip-value {
  color: #2ecc71;
  font-weight: 700;
}

