/* ═══════════════════════════════════════
   LAYOUT — App structure, Stream, Chat
   ═══════════════════════════════════════ */
#app {
  position: relative;
  z-index: 1;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#top-bar {
  position: relative;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  flex-shrink: 0;
  gap: 8px;
}

#top-bar-left {
  display: flex;
  align-items: center;
  gap: 8px;
}

#logo-text {
  font-size: 14px;
  font-weight: 800;
  letter-spacing: 2px;
  background: linear-gradient(135deg, #a78bff, #6b3fcc);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

#top-bar-right {
  display: flex;
  align-items: center;
  gap: 4px;
}

.top-icon {
  width: 38px;
  height: 38px;
  border-radius: 12px;
  border: none;
  background: transparent;
  color: rgba(255,255,255,0.4);
  font-size: 18px;
  cursor: pointer;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
}
.top-icon.active {
  color: #fff;
  background: rgba(138,94,255,0.12);
}
.top-icon:hover { color: rgba(255,255,255,0.7); }
.top-icon:active { transform: scale(0.92); }

/* ─── Screen ─── */
#screen {
  flex: 1;
  position: relative;
  overflow: hidden;
}

.screen-page {
  display: none;
  position: absolute;
  inset: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.screen-page.active { display: flex; }

/* ─── HOME — Stream + Chat ─── */
#page-home { flex-direction: column; }

#chat-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  position: relative;
  overflow: hidden;
}

/* ─── STREAM WRAPPER (flotante, overlay fijo) ─── */
#stream-wrapper {
  position: relative;
  width: 100%;
  flex-shrink: 0;
  overflow: hidden;
  background: #000;
}

#stream-wrapper video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

/* ─── Fade-out en borde inferior del stream ─── */
#stream-fade {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 30%;
  background: linear-gradient(to bottom, transparent, var(--bg));
  pointer-events: none;
  z-index: 2;
}

/* ─── STREAM CONTROLS (semi-transparentes) ─── */
#stream-controls {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  padding: 6px 8px;
  gap: 4px;
  z-index: 3;
  background: linear-gradient(to bottom, rgba(0,0,0,0.4), transparent);
  opacity: 0;
  transition: opacity 0.3s;
}
#stream-wrapper:hover #stream-controls,
#stream-wrapper:focus-within #stream-controls { opacity: 1; }

#live-badge {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 1px;
  color: #ff5555;
  padding: 3px 10px;
  border-radius: 20px;
  background: rgba(255,50,50,0.08);
  border: 0.5px solid rgba(255,50,50,0.12);
}
#live-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #ff3333;
  animation: livePulse 1.5s ease-in-out infinite;
}
@keyframes livePulse {
  0%, 100% { opacity: 1; transform: scale(1); box-shadow: 0 0 4px rgba(255,50,50,0.4); }
  50% { opacity: 0.5; transform: scale(0.8); box-shadow: 0 0 8px rgba(255,50,50,0.2); }
}

.ctrl-btn {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  border: none;
  background: rgba(0,0,0,0.3);
  backdrop-filter: blur(4px);
  color: white;
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s;
}
.ctrl-btn:hover { background: rgba(255,255,255,0.1); }
.ctrl-btn:active { transform: scale(0.9); }

/* ─── CHAT INPUT ─── */
#chat-input-wrap {
  position: relative;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  padding-bottom: max(6px, env(safe-area-inset-bottom));
  flex-shrink: 0;
}
#chat-input {
  flex: 1;
  padding: 10px 14px;
  border-radius: 14px;
  border: 0.5px solid rgba(255,255,255,0.06);
  background: rgba(255,255,255,0.04);
  color: #fff;
  font-size: 14px;
  outline: none;
  font-family: var(--font);
  transition: border-color 0.15s;
}
#chat-input:focus { border-color: rgba(138,94,255,0.25); }
#chat-input::placeholder { color: rgba(255,255,255,0.2); }

#send-btn {
  width: 42px;
  height: 42px;
  border-radius: 14px;
  border: none;
  background: linear-gradient(135deg, rgba(138,94,255,0.8), rgba(107,63,204,0.8));
  color: #fff;
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s;
  flex-shrink: 0;
}
#send-btn:hover { opacity: 0.85; }
#send-btn:active { transform: scale(0.92); }

#gif-btn {
  width: 38px;
  height: 38px;
  border-radius: 12px;
  border: none;
  background: transparent;
  color: rgba(255,255,255,0.3);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s;
  flex-shrink: 0;
}
#gif-btn:hover { color: rgba(255,255,255,0.6); background: rgba(255,255,255,0.04); }
#gif-btn.active { color: var(--accent-light); background: rgba(138,94,255,0.08); }
    #chat-container {
      flex: 1;
      display: flex;
      flex-direction: column;
      overflow: hidden;
      min-height: 0;
      margin: 0;
      padding: 0;
      position: relative;
      background: transparent;
    }

    #chat-messages {
      flex: 1;
      overflow-y: auto;
      padding: calc(28vh + 4px) 4px 2px;
      display: flex;
      flex-direction: column;
      position: relative;
      z-index: 1;
      scroll-behavior: smooth;
      /* Los mensajes se difuminan al acercarse al video */
      -webkit-mask-image: linear-gradient(to bottom, transparent 0%, black 8%, black 100%);
      mask-image: linear-gradient(to bottom, transparent 12%, black 18%, black 100%);
    }
    #chat-messages::-webkit-scrollbar { width: 3px; }
    #chat-messages::-webkit-scrollbar-track { background: transparent; }
    #chat-messages::-webkit-scrollbar-thumb { background: rgba(138,94,255,0.2); border-radius: 3px; }

    /* ─── AVATAR de otros usuarios ─── */
    .msg-avatar {
      width: 20px; height: 20px;
      border-radius: 50%;
      flex-shrink: 0;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 7px;
      font-weight: 700;
      color: #fff;
      text-shadow: 0 1px 2px rgba(0,0,0,0.3);
      box-shadow: 0 2px 8px rgba(0,0,0,0.15);
      border: 1px solid rgba(255,255,255,0.06);
      overflow: hidden;
    }
    .msg-avatar img {
      width: 100%; height: 100%;
      object-fit: cover;
      display: block;
    }

    /* ─── WRAPPER para alinear avatar + burbuja ─── */
    .msg-row {
      display: flex;
      align-items: flex-end;
      gap: 4px;
      max-width: 88%;
      margin-bottom: 1px;
    }
    .msg-row.own {
      align-self: flex-end;
      flex-direction: row-reverse;
    }
    .msg-row.other {
      align-self: flex-start;
    }

    /* Separación extra entre distintos usuarios */
    .msg-row.group-start {
      margin-top: 4px;
    }

    /* ─── BURBUJAS GLASS ULTRA ─── */
    .msg {
      padding: 3px 7px;
      border-radius: 10px;
      font-size: 11px;
      line-height: 1.3;
      word-wrap: break-word;
      position: relative;
      max-width: 100%;
      backdrop-filter: blur(10px);
      -webkit-backdrop-filter: blur(10px);
      animation: msgFloat 0.35s cubic-bezier(0.16, 1, 0.3, 1);
    }
    @keyframes msgFloat {
      0% { opacity: 0; transform: translateY(12px) scale(0.95); }
      100% { opacity: 1; transform: translateY(0) scale(1); }
    }

    /* ─── BURBUJA PROPIA (derecha) ─── */
    .msg.own {
      background: linear-gradient(135deg, rgba(138,94,255,0.2), rgba(107,63,204,0.12));
      border: 0.5px solid rgba(138,94,255,0.15);
      border-bottom-right-radius: 4px;
      box-shadow: 
        0 4px 16px rgba(138,94,255,0.08),
        inset 0 1px 0 rgba(255,255,255,0.06);
    }
    .msg.own::after {
      content: '';
      position: absolute;
      bottom: 0;
      right: -5px;
      width: 10px;
      height: 10px;
      background: rgba(138,94,255,0.15);
      border-radius: 0 0 10px 0;
      clip-path: polygon(0 0, 100% 100%, 0 100%);
      border-right: 0.5px solid rgba(138,94,255,0.1);
      border-bottom: 0.5px solid rgba(138,94,255,0.1);
    }
    /* Burbuja seguida (mismo usuario) — sin cola */
    .msg.own.tail-hide::after { display: none; }
    .msg.own.tail-hide {
      border-bottom-right-radius: 18px;
    }

    /* ─── BURBUJA DE OTRO (izquierda) ─── */
    .msg.other {
      background: rgba(255,255,255,0.04);
      border: 0.5px solid rgba(255,255,255,0.06);
      border-bottom-left-radius: 4px;
      box-shadow: 
        0 4px 16px rgba(0,0,0,0.1),
        inset 0 1px 0 rgba(255,255,255,0.04);
    }
    .msg.other::after {
      content: '';
      position: absolute;
      bottom: 0;
      left: -5px;
      width: 10px;
      height: 10px;
      background: rgba(255,255,255,0.04);
      border-radius: 0 0 0 10px;
      clip-path: polygon(100% 0, 0 100%, 100% 100%);
      border-left: 0.5px solid rgba(255,255,255,0.04);
      border-bottom: 0.5px solid rgba(255,255,255,0.04);
    }
    .msg.other.tail-hide::after { display: none; }
    .msg.other.tail-hide {
      border-bottom-left-radius: 18px;
    }

    /* Nickname (solo primer msg del grupo de otro user) */
    .msg-nick {
      font-size: 9px;
      font-weight: 700;
      color: var(--accent-light);
      margin-bottom: 1px;
      letter-spacing: -0.2px;
    }
    .msg-text { 
      color: var(--text);
      text-shadow: 0 1px 2px rgba(0,0,0,0.2);
    }

    /* ─── TIMESTAMP (oculto por defecto, visible al hover o en grupo) ─── */
    .msg-time {
      font-size: 7px;
      color: rgba(255,255,255,0.2);
      margin-top: 1px;
      text-align: right;
      letter-spacing: 0.2px;
      opacity: 0;
      transition: opacity 0.2s;
    }
    .msg-row:hover .msg-time {
      opacity: 1;
    }
    .msg-time.visible {
      opacity: 0.5;
    }

    /* Checkmark de enviado */
    .msg-check {
      display: inline-block;
      margin-left: 2px;
      font-size: 10px;
      opacity: 0.5;
    }

    /* ─── BOTÓN SCROLL TO BOTTOM ─── */
    #scroll-bottom {
      position: absolute;
      bottom: 62px;
      right: 16px;
      width: 36px;
      height: 36px;
      border-radius: 50%;
      background: rgba(138,94,255,0.2);
      backdrop-filter: blur(12px);
      -webkit-backdrop-filter: blur(12px);
      border: 0.5px solid rgba(138,94,255,0.1);
      color: #fff;
      font-size: 14px;
      cursor: pointer;
      display: none;
      align-items: center;
      justify-content: center;
      z-index: 5;
      transition: all 0.25s cubic-bezier(0.16, 1, 0.3, 1);
      box-shadow: 0 4px 16px rgba(0,0,0,0.2);
      animation: floatUp 0.3s ease-out;
    }
    #scroll-bottom.show { display: flex; }
    #scroll-bottom:active { transform: scale(0.9); }
    @keyframes floatUp {
      0% { opacity: 0; transform: translateY(10px) scale(0.8); }
      100% { opacity: 1; transform: translateY(0) scale(1); }
    }

    /* ─── CHAT INPUT — Crystal Glass ─── */
    #chat-input-wrap {
      display: flex;
      align-items: center;
      gap: 8px;
      padding: 8px 10px max(10px, env(safe-area-inset-bottom, 10px));
      flex-shrink: 0;
      position: sticky;
      bottom: 0;
      z-index: 30;
      background: transparent;
    }
    #chat-input {
      flex: 1;
      background: rgba(255,255,255,0.04);
      backdrop-filter: blur(12px);
      -webkit-backdrop-filter: blur(12px);
      border: 0.5px solid rgba(255,255,255,0.08);
      border-radius: 22px;
      padding: 11px 18px;
      color: #fff;
      font-size: 14px;
      font-family: var(--font);
      outline: none;
      transition: all 0.25s ease;
      box-shadow: 
        inset 0 1px 2px rgba(255,255,255,0.04),
        0 2px 8px rgba(0,0,0,0.1);
    }
    #chat-input:focus { 
      border-color: rgba(138,94,255,0.3);
      background: rgba(255,255,255,0.06);
      box-shadow: 
        inset 0 1px 2px rgba(255,255,255,0.06),
        0 0 20px rgba(138,94,255,0.06);
    }
    #chat-input::placeholder { color: rgba(255,255,255,0.2); }

    #send-btn {
      width: 38px; height: 38px;
      border-radius: 50%;
      background: linear-gradient(135deg, var(--accent), #6b3fcc);
      border: none;
      color: #fff;
      font-size: 16px;
      cursor: pointer;
      display: flex;
      align-items: center;
      justify-content: center;
      flex-shrink: 0;
      transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
      box-shadow: 
        0 4px 16px rgba(138,94,255,0.25),
        inset 0 1px 0 rgba(255,255,255,0.15);
      position: relative;
      overflow: hidden;
    }
    #send-btn::before {
      content: '';
