:root {
  --bg: #0b0d12;
  --surface: #141824;
  --surface-2: #1c2233;
  --border: #262e44;
  --text: #eef1f8;
  --text-dim: #8b93a7;
  --accent: #7c5cff;
  --accent-hover: #9078ff;
  --live: #ff4d5e;
  --radius: 12px;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

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

a { color: inherit; text-decoration: none; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 28px;
  border-bottom: 1px solid var(--border);
  background: rgba(11, 13, 18, 0.85);
  backdrop-filter: blur(8px);
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 18px;
  letter-spacing: 0.3px;
}

.brand .bolt {
  color: var(--accent);
  font-size: 20px;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  border: none;
  border-radius: 10px;
  padding: 10px 18px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, transform 0.05s;
  background: var(--surface-2);
  color: var(--text);
}

.btn:hover { background: var(--border); }
.btn:active { transform: scale(0.98); }

.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }

.btn-danger { background: var(--live); color: #fff; }
.btn-danger:hover { background: #ff6572; }

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

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 28px;
}

.page-title {
  font-size: 22px;
  font-weight: 700;
  margin-bottom: 4px;
}

.page-sub {
  color: var(--text-dim);
  font-size: 14px;
  margin-bottom: 24px;
}

/* ------- Browse grid ------- */

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 20px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  transition: border-color 0.15s, transform 0.15s;
}

.card:hover {
  border-color: var(--accent);
  transform: translateY(-2px);
}

.card .thumb {
  position: relative;
  aspect-ratio: 16 / 9;
  background: var(--surface-2);
}

.card .thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.badge-live {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--live);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.8px;
  padding: 3px 8px;
  border-radius: 6px;
}

.badge-live::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #fff;
  animation: pulse 1.4s infinite;
}

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

.card .thumb .badge-live {
  position: absolute;
  top: 10px;
  left: 10px;
}

.card .thumb .viewers {
  position: absolute;
  bottom: 10px;
  left: 10px;
  background: rgba(0, 0, 0, 0.7);
  font-size: 12px;
  padding: 3px 8px;
  border-radius: 6px;
}

.card .meta { padding: 14px; }

.card .meta .title {
  font-weight: 600;
  font-size: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.card .meta .who {
  color: var(--text-dim);
  font-size: 13px;
  margin-top: 4px;
}

.empty {
  text-align: center;
  padding: 80px 20px;
  color: var(--text-dim);
}

.empty .icon { font-size: 42px; margin-bottom: 12px; }
.empty h2 { color: var(--text); font-size: 18px; margin-bottom: 6px; }

/* ------- Player / broadcast ------- */

.stage {
  max-width: 960px;
  margin: 0 auto;
  padding: 28px;
}

.video-shell {
  position: relative;
  background: #000;
  border-radius: var(--radius);
  overflow: hidden;
  border: 1px solid var(--border);
}

.video-shell video {
  width: 100%;
  aspect-ratio: 16 / 9;
  display: block;
  background: #000;
}

.video-shell .overlay-badges {
  position: absolute;
  top: 12px;
  left: 12px;
  display: flex;
  gap: 8px;
  align-items: center;
}

.overlay-badges .viewers {
  background: rgba(0, 0, 0, 0.7);
  font-size: 12px;
  padding: 4px 9px;
  border-radius: 6px;
}

.stream-info {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-top: 16px;
}

.stream-info .title { font-size: 18px; font-weight: 700; }
.stream-info .who { color: var(--text-dim); font-size: 14px; margin-top: 3px; }

/* ------- Forms ------- */

.form-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 22px;
  margin-bottom: 20px;
}

.field { margin-bottom: 16px; }

.field label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-dim);
  margin-bottom: 6px;
}

.field input {
  width: 100%;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 14px;
  padding: 10px 12px;
  outline: none;
}

.field input:focus { border-color: var(--accent); }

.controls {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}

.status-line {
  font-size: 13px;
  color: var(--text-dim);
  min-height: 20px;
  margin-top: 12px;
}

.status-line.error { color: var(--live); }

/* ------- Chat ------- */

.watch-stage { max-width: 1280px; }

.watch-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  gap: 20px;
  align-items: stretch;
}

/* No chat yet (broadcast page before going live) — use the full width */
.watch-layout:has(> .chat-panel.hidden) {
  grid-template-columns: minmax(0, 1fr);
}

/* Inside the grid the chat matches the video column's height */
.watch-layout .chat-panel { height: auto; min-height: 420px; }

.stream-controls { margin-top: 16px; }

@media (max-width: 900px) {
  .watch-layout { grid-template-columns: minmax(0, 1fr); }
}

.chat-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  height: 480px;
}

.chat-panel.compact { height: 320px; margin-top: 16px; }

.chat-header {
  padding: 12px 14px;
  font-weight: 600;
  font-size: 14px;
  border-bottom: 1px solid var(--border);
}

.chat-messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 10px 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 14px;
}

.chat-msg { line-height: 1.45; overflow-wrap: anywhere; }

.chat-msg .chat-name {
  font-weight: 700;
  color: var(--accent-hover);
  margin-right: 6px;
}

.chat-msg.streamer .chat-name { color: var(--live); }

.badge-streamer {
  display: inline-block;
  background: var(--live);
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.5px;
  padding: 1px 5px;
  border-radius: 4px;
  margin-right: 6px;
  vertical-align: 1px;
}

.chat-msg.system { color: var(--text-dim); font-style: italic; }

.chat-empty { color: var(--text-dim); font-size: 13px; text-align: center; margin-top: 20px; }

.chat-input {
  display: flex;
  gap: 8px;
  padding: 10px;
  border-top: 1px solid var(--border);
}

.chat-input input {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 9px 10px;
  font-size: 13px;
  outline: none;
  min-width: 0;
}

.chat-input input:focus { border-color: var(--accent); }

.chat-input .name-input { width: 90px; flex-shrink: 0; }
.chat-input .text-input { flex: 1; }
.chat-input .btn { padding: 9px 14px; flex-shrink: 0; }

/* ------- Immersive mobile view: TikTok-style (watch + broadcast) ------- */

.mobile-top { display: none; }
.unmute-hint { display: none; }

@media (max-width: 900px) {
  body.immersive { background: #000; overflow: hidden; }
  .immersive .topbar { display: none; }
  .immersive .stage { padding: 0; max-width: none; }
  .immersive .watch-layout { display: block; }
  .immersive .page-title,
  .immersive .page-sub { display: none; }

  /* Full-bleed video, cropped to fill like TikTok */
  .immersive .video-shell {
    position: fixed;
    inset: 0;
    border: none;
    border-radius: 0;
    z-index: 1;
  }
  .immersive .video-shell video {
    width: 100%;
    height: 100dvh;
    aspect-ratio: auto;
    object-fit: cover;
  }

  /* Broadcast page: go-live card floats over the camera preview */
  .immersive .form-card {
    position: fixed;
    left: 12px;
    right: 12px;
    bottom: calc(12px + env(safe-area-inset-bottom));
    margin: 0;
    z-index: 4;
    background: rgba(20, 24, 36, 0.88);
    backdrop-filter: blur(8px);
  }
  .immersive .overlay-badges {
    top: calc(10px + env(safe-area-inset-top));
    z-index: 2;
  }
  .immersive .stream-controls {
    position: fixed;
    top: calc(10px + env(safe-area-inset-top));
    right: 12px;
    margin: 0;
    z-index: 3;
    flex-direction: column;
    align-items: flex-end;
  }
  .immersive .stream-controls .btn { padding: 8px 12px; font-size: 13px; }
  .immersive .status-line {
    position: fixed;
    left: 12px;
    right: 12px;
    bottom: calc(64px + env(safe-area-inset-bottom));
    margin: 0;
    z-index: 2;
    text-shadow: 0 1px 3px rgba(0, 0, 0, 0.8);
  }

  /* Watch page: metadata lives in the top overlay instead */
  .watch-body .overlay-badges,
  .watch-body .stream-info,
  .watch-body .status-line { display: none; }

  /* Top overlay: title, broadcaster, live badge, viewers, close */
  .immersive .mobile-top {
    display: flex;
    align-items: center;
    gap: 8px;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    padding: calc(10px + env(safe-area-inset-top)) 12px 24px;
    background: linear-gradient(rgba(0, 0, 0, 0.65), transparent);
    z-index: 2;
  }
  .mobile-top .mt-info { flex: 1; min-width: 0; }
  .mobile-top .mt-title {
    font-weight: 700;
    font-size: 15px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    text-shadow: 0 1px 3px rgba(0, 0, 0, 0.6);
  }
  .mobile-top .mt-who {
    font-size: 12px;
    color: rgba(255, 255, 255, 0.75);
    text-shadow: 0 1px 3px rgba(0, 0, 0, 0.6);
  }
  .mobile-top .viewers {
    background: rgba(0, 0, 0, 0.55);
    font-size: 12px;
    padding: 3px 8px;
    border-radius: 6px;
  }
  .mobile-top .mt-close {
    font-size: 18px;
    padding: 4px 8px;
    color: #fff;
    text-shadow: 0 1px 3px rgba(0, 0, 0, 0.6);
  }

  .immersive .unmute-hint {
    display: block;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(0, 0, 0, 0.6);
    color: #fff;
    border: 1px solid rgba(255, 255, 255, 0.25);
    border-radius: 999px;
    padding: 10px 18px;
    font-size: 14px;
    z-index: 3;
  }

  /* Chat floats over the bottom of the video, TikTok-style */
  .immersive .chat-panel {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 2;
    background: transparent;
    border: none;
    border-radius: 0;
    height: auto;
  }
  .immersive .chat-header { display: none; }
  .immersive .chat-messages {
    max-height: 30dvh;
    padding: 0 12px 6px;
    /* Older messages fade out toward the top */
    -webkit-mask-image: linear-gradient(transparent, black 22%);
    mask-image: linear-gradient(transparent, black 22%);
  }
  .immersive .chat-msg {
    align-self: flex-start;
    background: rgba(0, 0, 0, 0.45);
    padding: 6px 10px;
    border-radius: 12px;
    font-size: 13px;
    max-width: 85%;
  }
  .immersive .chat-empty {
    align-self: flex-start;
    background: rgba(0, 0, 0, 0.45);
    padding: 6px 10px;
    border-radius: 12px;
  }
  .immersive .chat-input {
    border: none;
    padding: 8px 12px calc(10px + env(safe-area-inset-bottom));
  }
  .immersive .chat-input input {
    background: rgba(255, 255, 255, 0.14);
    border-color: transparent;
    border-radius: 999px;
    color: #fff;
  }
  .immersive .chat-input input::placeholder { color: rgba(255, 255, 255, 0.6); }
  .immersive .chat-input .name-input { width: 76px; }
  .immersive .chat-input .btn { border-radius: 999px; }
}

.hidden { display: none !important; }

.center-note {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-dim);
}

.center-note h2 { color: var(--text); margin-bottom: 8px; }
.center-note .btn { margin-top: 18px; }
